2016-08-12 74 views
0

更新我的代碼的基礎上,以色列Unterman的回覆:現在蟒蛇3.4,設置QWidgets.QMessageBox

誤差類是

from PyQt5 import QtWidgets 
from PyQt5.QtWidgets import QMainWindow 

class Error(QtWidgets.QMainWindow): 
    reply = False 
    last_reply_id = None 
    last_id = 0 

    def __init__(self, error_code_string, parent=None): 
     super().__init__(parent) 
     QtWidgets.QMessageBox.warning(self, "Warnung", error_code_string, QtWidgets.QMessageBox.Ok) 
     id = give_id(self) 

    def give_id(self): 
     self.last_id += 1 
     return self.last_id 

    def give_reply(self): 
     if last_id == last_reply_id: 
      return self.reply 
     else: 
      return None 

    def set_reply(self, button, id): 
     if button in (QMessageBox.Ok, QMessageBox.Yes): 
      reply = True 
     else: 
      reply = False 
     self.last_reply_id = id 
     return reply 

和測試腳本帶有

from ErrorHandling import Error 

Error('Test') 

如果我使用的正常碼(實際上是相同的代碼,只是包裹在一個類)出現的消息,然後在該行

id = give_id(self) 

代碼停止,不從蟒蛇任何errormessage的,只是:

Process finished with exit code 1 

如果我使用的測試腳本,沒有什麼(無QMessageBox提示!)不是這樣的:

Process finished with exit code 1 

如果我調試代碼,INIT()獲取相同的對象和變量,但

super().__init__(parent) 

失敗,沒有任何消息。 那麼錯誤或差異在哪裏。

這裏類的shortend版本(這是太長,在這裏展示的所有代碼),從「錯誤」的作品幾乎罰款:

from ErrorHandling import Error 
class MainWindow(QWidget): 

    def __init__(self, parent=None): 
     super().__init__(parent) 
     # set some variables 
     self.create_layout() 

    def create_layout(self): 
     # creates a GUI using QWidgets with some Inputboxes and one button 

[...]  

    def button_start_clicked(self): 
     Error('Check the input') 

這裏是老問題:

我QtWidgets.QMessageBox的設置有問題。 所有代碼都遵循說明。

ErrorHandling-Modul應該給出關於錯誤的消息。 如果需要,也可以提出一個問題。 在捕捉異常的情況下,函數ErrorMsg.errorMessage是從其他Moduls調用的。 會增加更多功能。

如果我運行代碼出現以下錯誤:

Connected to pydev debugger (build 145.1504) 
Traceback (most recent call last): 
    File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.1.4\helpers\pydev\pydevd.py", line 1531, in <module> 
    globals = debugger.run(setup['file'], None, None, is_module) 
    File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.1.4\helpers\pydev\pydevd.py", line 938, in run 
    pydev_imports.execfile(file, globals, locals) # execute the script 
    File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.1.4\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile 
    exec(compile(contents+"\n", file, 'exec'), glob, loc) 
    File "C:/Quellcode/AllgTest.py", line 5, in <module> 
    reply = errm.errorMessage('Test') 
    File "C:/Quellcode\ErrorHandling.py", line 20, in errorMessage 
    msg_box.setIcon(QMessageBox.Warning) 
    TypeError: QMessageBox.setIcon(QMessageBox.Icon): first argument of unbound method must have type 'QMessageBox' 

Process finished with exit code 1 

我嘗試頗有些變化和GOOGLE了,但我不知道是什麼問題,因爲我發現,正在使用線路 QMessageBox提示一些例子.setIcon(QMessageBox.Icon) 那麼我的錯誤在哪裏?

而現在的代碼:

有以下testscript來測試我的ERRORMSG級

from ErrorHandling import ErrorMsg 
errm = ErrorMsg() 
reply = errm.errorMessage('Test') 

這裏是我的ErrorHandling中-MODUL

from PyQt5.QtWidgets import QMessageBox 
from PyQt5.QtWidgets import QMainWindow 

class ErrorMsg(QMainWindow): 
    def __init__(self): 
     pass 

    def giveback(self,button): 
     if button in (QMessageBox.Ok, QMessageBox.Yes): 
      reply = True 
     else: 
      reply = False 
     return reply 

    def errorMessage(self, error_msg, buttons='OK'): 
     msg_box = QMessageBox 
     msg_box.setIcon(QMessageBox.Warning) 
     msg_box.setWindowTitle('Warning') 
     if buttons == 'OK': 
      msg_box.setStandardButtons(QMessageBox.Ok) 
     elif buttons == 'YesNo': 
      msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No) 
     else: 
      error_msg = 'Unknown Button >' + buttons + '<, use >OK<or>YesNo<' 
      raise ValueError(error_msg) 
     msg_box.setText(error_msg) 
     clicked_button = msg_box.exec() 
     return giveback(clicked_button) 

感謝您的幫助

James

回答

0

您沒有創建消息框的對象。要創建一個對象使用:

msg_box = QMessageBox() 

但你don'y需要經過這一切,因爲QMessageBox有用於顯示信息,您可以在QMessageBox類直接調用靜態函數。例如:

QMessageBox.warning(None, 'title', 'msg') 

您也有過一些butons控制,見QMessageBox

+0

正確的,但他還需要使用'調用'giveback'方法時self';) –

+0

@Israel Unterman 我需要的功能,因爲在未來的發展中,有必要知道,什麼按鈕被點擊,所以我需要一個返回值。 –