gpt4 book ai didi

python - PyQt5。对话框窗口不显示。改为关闭主窗口

转载 作者:太空宇宙 更新时间:2023-11-03 14:40:54 26 4
gpt4 key购买 nike

我在 PyQt5 中编写 gui 应用程序时遇到了问题。我试图通过单击“addclientbutton”打开第二个(对话框)窗口。但它只是关闭主窗口。我在这里创建了它:

class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(281, 176)
self.comboBox = QtWidgets.QComboBox(Dialog)
self.comboBox.setGeometry(QtCore.QRect(90, 10, 71, 22))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox_2 = QtWidgets.QComboBox(Dialog)
self.comboBox_2.setGeometry(QtCore.QRect(10, 10, 71, 22))
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.addItem("")
self.dateTimeEdit = QtWidgets.QDateTimeEdit(Dialog)
self.dateTimeEdit.setGeometry(QtCore.QRect(10, 60, 194, 22))
self.dateTimeEdit.setObjectName("dateTimeEdit")
self.label_2 = QtWidgets.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(10, 40, 91, 16))
self.label_2.setObjectName("label_2")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(150, 110, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.label_3 = QtWidgets.QLabel(Dialog)
self.label_3.setGeometry(QtCore.QRect(150, 90, 121, 16))
self.label_3.setObjectName("label_3")
self.lineEdit_2 = QtWidgets.QLineEdit(Dialog)
self.lineEdit_2.setGeometry(QtCore.QRect(10, 110, 113, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
self.label_4 = QtWidgets.QLabel(Dialog)
self.label_4.setGeometry(QtCore.QRect(10, 90, 111, 16))
self.label_4.setObjectName("label_4")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(150, 140, 111, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(Dialog)
self.pushButton_2.setGeometry(QtCore.QRect(10, 140, 111, 23))
self.pushButton_2.setObjectName("pushButton_2")

self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)

这里我创建了按钮本身:

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(803, 803)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
self.calendarWidget.setGeometry(QtCore.QRect(0, 0, 331, 191))
self.calendarWidget.setObjectName("calendarWidget")
self.addclient = QtWidgets.QPushButton(self.centralwidget)
self.addclient.setGeometry(QtCore.QRect(0, 350, 331, 81))
font = QtGui.QFont()
font.setFamily("Verdana")
font.setPointSize(10)
self.addclient.setFont(font)
self.addclient.setObjectName("addclient")

然后我写了这个函数:

import sys
from interface import *
from dialog import *
from PyQt5 import QtCore, QtGui, QtWidgets

class MyWin(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)

self.ui.addclient.clicked.connect(self.addclientfunction)

def addclientfunction(self):
self.Ui_Dialog.Dialog.show()

if __name__=="__main__":
app = QtWidgets.QApplication(sys.argv)
myapp = MyWin()
myapp.show()
sys.exit(app.exec_())

所以,这就是 - 对于 PyQt 设计师的滥用行为表示歉意:D

最佳答案

您只需按照与主窗口相同的方式创建一个对话框类。所以这样的事情应该有效:

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

class MyWin(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MyWin, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.addclient.clicked.connect(self.addclientfunction)

def addclientfunction(self):
self.dialog = MyDialog()
self.dialog.show()

但请注意,您的问题中显示的 Ui_DialogUi_MainWindow 类不完整。我的回答假设您的完整 ui 类按预期工作。

关于python - PyQt5。对话框窗口不显示。改为关闭主窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46548555/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com