2017-06-21 29 views
2

這裏是我的代碼:QMessage是一類功能工作正常,但不是在單獨的函數

def er(): 
    print("connection error") 
    from PyQt5.QtWidgets import QMessageBox 
    msg = QMessageBox() 
    msg.setIcon(QMessageBox.Information) 
    msg.setText("Some text ") 
    msg.setInformativeText("info text") 
    msg.setWindowTitle("title") 
    msg.setStandardButtons(QMessageBox.Ok) 
    retval = msg.exec_() 
    print(retval) 
if __name__ == '__main__': 

    mac = (':'.join(['{:02x}'.format((getnode() >> i) & 0xff) for i in range(0,8 * 6, 8)][::-1])) 

    if mac == 'b8:e8:56:24:96:30': 
     print("OK") 
     some_function 
    else: 
     er() 

和錯誤是「QWidget的:前一個QWidget必須構造一個QApplication的」

回答

0

你必須首先使用QApplication初始化qt事件循環,如所示的錯誤消息,如

def er(): 
    print("connection error") 
    from PyQt5.QtWidgets import QMessageBox,QApplication 
    import sys 
    app = QApplication(sys.argv) 

    msg = QMessageBox() 
    msg.setIcon(QMessageBox.Information) 
    msg.setText("Some text ") 
    msg.setInformativeText("info text") 
    msg.setWindowTitle("title") 
    msg.setStandardButtons(QMessageBox.Ok) 
    retval = msg.exec_() 
    print(retval) 


if __name__ == '__main__': 

    mac = (':'.join(['{:02x}'.format((getnode() >> i) & 0xff) for i in range(0, 8 * 6, 8)][::-1])) 

    if mac == 'b8:e8:56:24:96:30': 
     print("OK") 
     some_function 
    else: 
     er() 
+0

非常感謝,它的工作原理類似Charm @Tobias – creator

相關問題