簡體   English   中英

如何使用 PyQt5 在第二個 window 中觸發按鈕的單擊事件

[英]How to trigger a click event for a push button in second window using PyQt5

我有一個主對話框 window 如下所示在此處輸入圖像描述

單擊確定按鈕后,將打開第二個 window,如下所示在此處輸入圖像描述

我需要觸發第二個window登錄按鈕frpm的點擊事件。 下面是我的代碼。 但我沒有觸發任何方法。

from .gisedify_support_dialog_login import Ui_Dialog
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'gisedify_support_dialog_base.ui'))
class GisedifySupportDialog(QtWidgets.QDialog, FORM_CLASS):
 def __init__(self, parent=None):
    """Constructor."""
    super(GisedifySupportDialog, self).__init__(parent)
    # Set up the user interface from Designer through FORM_CLASS.
    # After self.setupUi() you can access any designer object by doing
    # self.<objectname>, and you can use autoconnect slots - see
    # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
    # #widgets-and-dialogs-with-auto-connect
    self.setupUi(self)

  def open_login_dialog(self):
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.exec_()
    ui.login_button.clicked.connect(self.login)

  def login(self):
    print('success')

class Login_Dialog(QtWidgets.QDialog,Ui_Dialog):
 def __init__(self, parent=None):
    super(Login_Dialog, self).__init__(parent)

QDialog.exec_()將阻塞,直到用戶關閉對話框,因此您需要在調用Dialog.exec_()之前設置任何信號槽連接。 關閉對話框時,如果接受了對話框,則返回 1,否則返回 0。 關閉對話框不會破壞它(除非您設置了一個標志來這樣做),因此您可以檢索在Dialog.exec_()返回后輸入的數據。

因此,與其將插槽連接到主 window 中的對話框按鈕按鈕,不如將QDialog子類化,使用 Qt 設計器文件設置 ui,然后將button.clicked信號連接到QDialog.accept插槽。 然后在主小部件中,您可以像以前一樣調用Dialog.exec_()並在之后檢索信息,例如

from PyQt5 import QtWidgets, QtCore, QtGui


class Login_Dialog(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self, parent = None):
        super().__init__(parent)
        self.setupUi(self)
        self.login_button.clicked.connect(self.accept)


class Widget(QtWidgets.QWidget):
    def __init__(self, parent = None):
        super().__init__(parent)
        # setup ui as before

    def get_login(self):
        dialog = Login_Dialog(self)
        if dialog.exec_():
            # get activation key from dialog
            # (I'm assuming here that the line edit in your dialog is assigned to dialog.line_edit)  
            self.activation_key = dialog.line_edit.text()
            self.login()

    def login(self)
        print(f'The activation_key you entered is {self.activation_key}')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM