简体   繁体   English

如何使用 PyQt5 在第二个 window 中触发按钮的单击事件

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

I have a main dialog window as shown below我有一个主对话框 window 如下所示在此处输入图像描述

Once the OK button is clicked, second window will open as shown below单击确定按钮后,将打开第二个 window,如下所示在此处输入图像描述

I need to trigger the click event of login button frpm the second window.我需要触发第二个window登录按钮frpm的点击事件。 Below is my code.下面是我的代码。 but i doesnt trigger any method.但我没有触发任何方法。

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_() will block until the dialog is closed by the user so you would need to set up any signal-slot connections before you call Dialog.exec_() . QDialog.exec_()将阻塞,直到用户关闭对话框,因此您需要在调用Dialog.exec_()之前设置任何信号槽连接。 When a dialog is closed, it returns 1 when de dialog was accepted, and 0 if not.关闭对话框时,如果接受了对话框,则返回 1,否则返回 0。 Closing the dialog does not detroy it (unless you set a flag to do so), so you can retrieve the data that was entered after Dialog.exec_() returns.关闭对话框不会破坏它(除非您设置了一个标志来这样做),因此您可以检索在Dialog.exec_()返回后输入的数据。

So, instead of connecting a slot to the dialog button buttonin the main window, you could instead subclass QDialog , setup the ui using your Qt Designer files, and connect the button.clicked signal to the QDialog.accept slot.因此,与其将插槽连接到主 window 中的对话框按钮按钮,不如将QDialog子类化,使用 Qt 设计器文件设置 ui,然后将button.clicked信号连接到QDialog.accept插槽。 Then in the main widget you can call Dialog.exec_() as before and retrieve the information afterwards, eg然后在主小部件中,您可以像以前一样调用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