简体   繁体   中英

Pop up warning box

I have a method that checks that a pyqt spinbox value has been entered, the required values are 1 through 18 inclusive so I initiate the spinbox with the value 0 so that I can easily tell its not been changed to the correct number. I thought that if the check finds its still 0 the user forget to set it and a warning pops up to let them know.. The problem I have is that the window pops up, user presses OK, the pop up closes but immediately opens again before the user has had time to set the correct value ... how can I have the pop up close and allow the user time to change the spinbox to the correct value before it checks for 0 and pops up again (if still not correct)

the signal that triggers group_check was originally triggering the pickFile method until I realised that the code executed whether Group No was set in the spinbox or not, I had tried to build the check into the pickFile method but then though it might be best to separate it out.

import sys
import time
from PyQt4 import QtGui, uic
import xlrd
import csv
import os
import re

qtCreatorFile = "UI1.ui" # Enter file here.
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)


class MyApp(QtGui.QMainWindow, Ui_MainWindow):    
    group = '' 
    group_1 = ''
    Var_1 = '' 
    Var_2 = ''
    Var_3 = ''
    error_string = ''


    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)

        self.file_picker_button.clicked.connect(self.group_check)
        self.radioButton_1.clicked.connect(self.onRadioButton1)       
        self.radioButton_2.clicked.connect(self.onRadioButton2)       
        self.radioButton_3.clicked.connect(self.onRadioButton3) 
        self.spinBox.valueChanged.connect(self.valuechange)


    def group_check(self):
        while True:
            if self.spinBox.value() == 0: 
                error_string ='You must select your\nGroup No first ! ok ? '
                self.error_msg(error_string)
            else:
                self.pickFile()

    def pickFile(self):
        while True:
            filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '.')
            if 'Monday' in filename:
                break
            elif 'Monday' not in filename or filename == '':
                error_string ='Wrong file ? \nWant to try again ?'
                self.error_msg1(error_string)
            else:
                self.filename = filename       
                x = first_file()
                x.csv_from_excel(filename)        

    def onRadioButton1(self, group):
        MyApp.Var_1 = 'PGDE SECONDARY ONLY'
        MyApp.Var_2 = 'MA4 ONLY'
        MyApp.Var_3 = 'PGDE'


    def onRadioButton2(self, group):
        MyApp.Var_1 = 'PGDE PRIMARY ONLY'
        MyApp.Var_2 = 'MA4 ONLY'
        MyApp.Var_3 = 'PGDE'


    def onRadioButton3(self, group):    
        MyApp.Var_1 = 'PGDE PRIMARY ONLY'
        MyApp.Var_2 = 'PGDE SECONDARY ONLY'
        MyApp.Var_3 = 'MA4'        


    def valuechange(self, value):
        MyApp.group_1 = ('Group '+ str(self.spinBox.value()))
        if self.spinBox.value() >= 10:
            MyApp.group = "1-9 ONLY"
        if self.spinBox.value() >= 1 and self.spinBox.value() <= 9:
            MyApp.group = "10-18 ONLY"

    def error_msg(self, error_string):
        choice = QtGui.QMessageBox.question(self, 'Error!', error_string)

Instead of using while True just display the error message and return from the function. Make them click the button again after they fix the error

def group_check(self):
    if self.spinBox.value() == 0: 
        error_string ='You must select your\nGroup No first ! ok ? '
        self.error_msg(error_string)
        return  
    self.pickFile()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM