gpt4 book ai didi

python - 如何在使用 PyQt5 打开新窗口时隐藏当前窗口

转载 作者:太空宇宙 更新时间:2023-11-04 02:55:53 26 4
gpt4 key购买 nike

我已将另一个文件链接到主窗口中的按钮。每次单击它时,新窗口都会打开,但旧窗口(主窗口)仍然存在。如何在不影响新窗口的情况下关闭或隐藏它。

PS:我是这里的新人。对我发布此查询时的任何愚蠢或错误表示歉意。谢谢!

相关代码如下:

    import sys  
from PyQt5 import QtCore, QtGui, QtWidgets
from create2 import Ui_MainWindow
from PyQt5.QtWidgets import QMainWindow, QWidget
from PyQt5.QtCore import QMargins

class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(813, 655)
self.widget = QtWidgets.QWidget(Form)
self.widget.setGeometry(QtCore.QRect(190, 50, 501, 591))
self.widget.setObjectName("widget")
.
.
.
def newusr_clk(self):
self.mainwin=QMainWindow()
self.ui=Ui_MainWindow()
self.ui.setupUi(self.mainwin)
self.mainwin.show()

最佳答案

这是一个完整的工作示例。主窗口只是一个QMainWindow。它将在单击按钮时打开一个 QDialog。他们的 ui 定义:

# file ui_main.py
from PyQt5 import QtCore, QtWidgets

class Ui_MainWindow:
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(442, 205)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(180, 80, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)

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

def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))

#file ui_dialog.py
from PyQt5 import QtCore, QtWidgets

class Ui_Dialog:
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(508, 300)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(150, 250, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)

在另一个文件中,定义 MainWindowPreferenceDialog 类:

# file main.py
from PyQt5.QtWidgets import QMainWindow, QApplication, QDialog

from ui_dialog import Ui_Dialog
from ui_main import Ui_MainWindow

class MainWindow(QMainWindow):

def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)

self.ui = Ui_MainWindow()
self.ui.setupUi(self)

self.ui.pushButton.clicked.connect(self.newusr_clk)

newuser_clk 插槽将隐藏MainWindow 并通过dialog.exec() 打开一个新的QDialog 框.当 QDialog 关闭时,MainWindow 再次显示。

    def newusr_clk(self):
self.hide()
dialog = PreferencesDialog(parent=self)
if dialog.exec():
pass # do stuff on success
self.show()

这是 PreferenceDialog 类:

class PreferencesDialog(QDialog):
def __init__(self, parent=None):
super(PreferencesDialog, self).__init__(parent)

self.ui = Ui_Dialog()
self.ui.setupUi(self)

下面是运行脚本的方法:

if __name__ == '__main__':
import sys

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

希望对您有所帮助!

关于python - 如何在使用 PyQt5 打开新窗口时隐藏当前窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42414784/

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