2017-02-28 7 views
0

私はユーザーインターフェイスのためのプログラムを書いています。私はQtの初心者ですが、特にQtデザイナーのおかげでそれを楽しんでいます。プログラムは次のようにする必要があります:2つのタブを持つメインウィンドウがあります。最初のタブはユーザー/パスワードフィールドと2つのボタン「ログイン」と「終了」を持つログインです。 「Exit」はもちろんアプリケーションを終了し、「Login」は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を取得する必要があるため、これが機能しているかどうかはわかりません。どこに置くべきですか?私がコードの「試し」の部分に入れたら、メインウィンドウのコンボボックスを初期化しようとすると参照テーブルが見つからないということがわかります。私は知っている、私は混乱です! :)

ありがとう、長い間ご質問申し訳ありません!

答えて

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が使用できるものを返します。

関連する問題