2017-02-28 125 views
0

我正在編寫一個用戶界面的程序。我對Qt相當陌生,但我很享受使用它,特別感謝Qt設計師。程序應該如下:它有一個帶有2個選項卡的主窗口。第一個選項卡是登錄名,包含用戶/密碼字段和兩個按鈕「登錄」和「退出」。 「退出」當然會退出應用程序,「登錄」將嘗試向服務器發送SOAP請求以進行登錄。如果不成功,將顯示一個帶有錯誤的彈出窗口。如果成功,則顯示帶有成功消息的彈出窗口,並且激活並顯示Tab2,其中用戶可以插入一些數據(具有值取決於程序一旦連接到服務器就下載的一些表的組合框)。這裏是一個修剪版本的代碼:PyQt5 GUI需要的結構建議

class Ui_PopupError(object): 
    def setupUi(self, Dialog): 
     Dialog.setObjectName("Dialog") 
     Dialog.resize(322, 101) 
     self.label = QtWidgets.QLabel(Dialog) 
     self.label.setGeometry(QtCore.QRect(60, 20, 221, 16)) 
     self.label.setObjectName("label") 
     self.pushButton = QtWidgets.QPushButton(Dialog) 
     self.pushButton.setGeometry(QtCore.QRect(120, 60, 75, 23)) 
     self.pushButton.setObjectName("pushButton") 
     self.retranslateUi(Dialog) 
     self.pushButton.clicked.connect(Dialog.reject) 
     QtCore.QMetaObject.connectSlotsByName(Dialog) 
    def retranslateUi(self, Dialog): 
     _translate = QtCore.QCoreApplication.translate 
     Dialog.setWindowTitle(_translate("Dialog", "Dialog")) 
     self.label.setText(_translate("Dialog", "Wrong Username or Password. Try Again")) 
     self.pushButton.setText(_translate("Dialog", "Ok")) 
class Ui_PopupSuccess(object): 
    def setupUi(self, Dialog): 
     Dialog.setObjectName("Dialog") 
     Dialog.resize(322, 101) 
     self.label = QtWidgets.QLabel(Dialog) 
     self.label.setGeometry(QtCore.QRect(60, 20, 221, 16)) 
     self.label.setObjectName("label") 
     self.pushButton = QtWidgets.QPushButton(Dialog) 
     self.pushButton.setGeometry(QtCore.QRect(120, 60, 75, 23)) 
     self.pushButton.setObjectName("pushButton") 
     self.retranslateUi(Dialog) 
     self.pushButton.clicked.connect(Dialog.accept) 
     QtCore.QMetaObject.connectSlotsByName(Dialog) 
    def retranslateUi(self, Dialog): 
     _translate = QtCore.QCoreApplication.translate 
     Dialog.setWindowTitle(_translate("Dialog", "Dialog")) 
     self.label.setText(_translate("Dialog", "Successfully connected.")) 
     self.pushButton.setText(_translate("Dialog", "Ok")) 

class Ui_MainWindow(object): 
    def LoginMacro(self): 
     Username = self.Usernamefield.text() 
     Password = self.Passwordfield.text() 
     req = urllib.request.Request("server") 
     body = SOAPBody 
     response = urllib.request.urlopen(req, body) 
     try: 
      soup = BeautifulSoup(response.read(), 'html.parser') 
      testo = soup.get_text().strip() 
      tree = ET.fromstring(testo) 
      sessionid = tree.attrib['sessionid'] 
      self.popup = QtWidgets.QDialog() 
      self.popupui = Ui_PopupSuccess() 
      self.popupui.setupUi(self.popup) 
      self.popup.show() 
      self.tabWidget.setCurrentIndex(1) 
      self.Recap_Tab.setEnabled(True) 
     except: 
      self.popup = QtWidgets.QDialog() 
      self.popupui = Ui_PopupError() 
      self.popupui.setupUi(self.popup) 
      self.popup.show() 
    def setupUi(self, MainWindow): 
     MainWindow.setObjectName("MainWindow") 
     MainWindow.resize(798, 867) 
     #MainWindow created with QT Designer (long code...) 
     self.Exit.clicked.connect(MainWindow.close) 
     self.Login.clicked.connect(self.LoginMacro) 
     #This is an example of the value which should feed the combobox: 
     self.Handling_Type.addItems(REF_UDF_VALUES['udf_reference_value'][REF_UDF_VALUES['udf_cd']=='Handling Type']) 
    def retranslateUi(self, MainWindow): 
     _translate = QtCore.QCoreApplication.translate 
     MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) 
     #Retranslate created with Qt designer....(cut) 

if __name__ == "__main__": 
    import sys 
    app = QtWidgets.QApplication(sys.argv) 
    MainWindow = QtWidgets.QMainWindow() 
    ui = Ui_MainWindow() 
    ui.setupUi(MainWindow) 
    MainWindow.show() 
    sys.exit(app.exec_()) 

我只是想問你,如果這是做到這一點的正確方法。特別是,我在理解將「真實」事情(與服務器通信)的代碼放在哪裏的問題。例如,我將登錄代碼放在MainWindow類中,但我不確定這是否正常工作,因爲我需要從該代碼中捕獲jsessionid,以便運行另一個下載表的代碼。我應該把這個放在哪裏?如果我把它放在代碼的「try」部分,它會告訴我,當它試圖在mainwindow中初始化組合框時,它不能找到參考表。我知道,我是一團糟! :)

謝謝,對不起長問題!

回答

1

與後端的通信應與GUI定義分開。我創建了一個名爲API的獨立類來處理請求/響應,併爲我的QMainWindow創建一個API實例以供使用。這裏我們想填充QComboBox與用戶列表的例子:

class MainWindow(QMainWindow): 
    def __init__(self, parent=None): 
     super(MainWindow, self).__init__(parent) 
     self.ui = Ui_MainWindow() 
     self.ui.setupUi(self) 

     self.api = API() 

     users = self.api.get_users() 
     self.user_widget = self.ui.userCombo 
     self.user_widget.setModel(UserListModel(users, self)) 

class API: 
    def __init__(self): 
     self.base_url = 'http://localhost:5000/api' 
     self.users_url = '/users' 
     self.timeout = 5 

    def get_users(self): 
     try: 
      r = requests.get(self.base_url + self.users_url, timeout=self.timeout) 
     except ConnectionError: 
      print("Could not connect to API") 
      return API.make_default_user_list() 

     users = r.json(object_hook=API.api_hook_handler) 

     return users 

正如你所看到的,MainWindow不一樣,如果用戶的數據請求成功或失敗在意。無論哪種方式,api.get_users()返回MainWindow可以使用的東西。