如何使用 PyQt5 在秒 window 内触发按钮的点击事件
How to trigger a click event for a push button in second window using PyQt5
我有一个主对话框 window 如下所示
单击“确定”按钮后,第二个 window 将打开,如下所示
我需要触发第二次登录按钮frpm的点击事件window。下面是我的代码。但是我没有触发任何方法。
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_()
之前设置任何信号槽连接。当对话框关闭时,它 returns 1 当对话框被接受,否则为 0。关闭对话框不会销毁它(除非您设置标志这样做),因此您可以检索在 Dialog.exec_()
returns 之后输入的数据。
因此,不是将插槽连接到主 window 中的对话框按钮按钮,而是可以子类化 QDialog
,使用 Qt Designer 文件设置 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}')
我有一个主对话框 window 如下所示
单击“确定”按钮后,第二个 window 将打开,如下所示
我需要触发第二次登录按钮frpm的点击事件window。下面是我的代码。但是我没有触发任何方法。
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_()
之前设置任何信号槽连接。当对话框关闭时,它 returns 1 当对话框被接受,否则为 0。关闭对话框不会销毁它(除非您设置标志这样做),因此您可以检索在 Dialog.exec_()
returns 之后输入的数据。
因此,不是将插槽连接到主 window 中的对话框按钮按钮,而是可以子类化 QDialog
,使用 Qt Designer 文件设置 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}')