简体   繁体   English

弹出警告框

[英]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. 我有一种方法可以检查是否已输入pyqt旋转框值,所需的值是1到18(含1和18),所以我将旋转框的值设置为0,这样我就可以很容易地告诉它未更改为正确的数字。 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) 我以为如果检查发现它仍然为0,用户会忘记设置它,并弹出警告让他们知道。。我的问题是窗口弹出,用户按OK,弹出窗口关闭,但立即又打开在用户有时间设置正确的值之前...如何关闭弹出窗口,并让用户有时间将旋转框更改为正确的值,然后再检查0并再次弹出(如果仍然不正确)

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. 触发group_check的信号最初是触发pickFile方法的,直到我意识到无论执行代码是否在Spinbox中设置了代码,我都尝试将检查构建到pickFile方法中,但是尽管也许最好将其分离出。

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. 而不是使用while True仅显示错误消息并从函数返回。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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