2016-03-08 184 views
1

我有一個方法,檢查pyqt spinbox值是否已輸入,所需的值是1到18,因此我啓動的值爲0的spinbox,以便我可以輕鬆地指出其未被更改爲正確的號碼。我認爲,如果檢查發現它仍然是0用戶忘記設置它,並彈出一個警告,讓他們知道.. 我的問題是彈出窗口,用戶按下確定,彈出窗口關閉,但立即打開再次在用戶有時間設置正確的值之前......我如何彈出關閉並允許用戶在檢查0並再次彈出之前將時間更改爲正確的值(如果仍然不正確)彈出警告框

觸發group_check的信號最初觸發pickFile方法,直到我意識到代碼執行是否在Spinnerbox中設置了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

爲什麼你在'while true'永久循環中擁有它?爲什麼不運行一次。另外,你可以顯示你的其他代碼嗎?如何調用'group_check'?它是否附加到信號?像按鈕點擊信號? –

+0

與此同時,在玩過一些遊戲後,我設法將按鈕變灰,直到spinbox值被設置。不是解決方案只是一個選擇。 – Stevo

+0

Brendan是對的,擺脫'while'循環。目前沒有辦法擺脫它,而且根本沒有理由擁有它。 –

回答

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

完美的工作,我認爲我已經複製了'while true',因爲它是所有組合的pickFile方法所需要的,但顯然沒有意識到它具有什麼作用,因爲當我刪除它彈出打開那麼當它關閉時,不管spinbox的值如何,執行都會繼續。 – Stevo

+0

我可以問,「回報」線路究竟有什麼影響? – Stevo