弹出警告框

时间:2016-03-08 21:40:04

标签: python pyqt

我有一个方法可以检查是否输入了pyqt spinbox值,所需的值是1到18,所以我启动了值为0的spinbox,以便我可以很容易地告诉它没有被更改为正确的数字。我想如果检查发现它仍为0,则用户忘记设置它并弹出警告让他们知道.. 我遇到的问题是窗口弹出,用户按OK,弹出窗口关闭但在用户有时间设置正确的值之前立即再次打开...如何让弹出窗口关闭并允许用户时间在检查0之前将spinbox更改为正确的值并再次弹出(如果仍然不正确)

触发group_check的信号最初是触发pickFile方法,直到我意识到代码执行了是否在旋转框中设置了Group No,我曾尝试将检查构建到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)

1 个答案:

答案 0 :(得分:2)

而不是使用while True只显示错误消息并从函数返回。让他们在修复错误后再次点击该按钮

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()