PyQt5 窗口间切换
Posted
技术标签:
【中文标题】PyQt5 窗口间切换【英文标题】:PyQt5 switch between windows 【发布时间】:2018-08-13 10:04:34 【问题描述】:我想根据图块创建菜单。现在我需要 PyQt5 概念,如何将 MainWindow 切换到 Window1/Window2/... 并返回 MainWindow 选项。我唯一实现的就是在顶部打开一个新窗口。我宁愿有单独的窗口,在那里我可以定义不同的功能。
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
from PyQt5.QtCore import pyqtSlot
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.title = "App"
self.top = 100
self.left = 100
self.width = 680
self.height = 500
self.InitUI()
def InitUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
buttonWindow1 = QPushButton('Window1', self)
buttonWindow1.move(100, 100)
buttonWindow1.clicked.connect(self.buttonWindow1_onClick)
buttonWindow2 = QPushButton('Window2', self)
buttonWindow2.move(100, 200)
buttonWindow2.clicked.connect(self.buttonWindow2_onClick)
self.show()
@pyqtSlot()
def buttonWindow1_onClick(self):
self.statusBar().showMessage("Switched to window 1")
@pyqtSlot()
def buttonWindow2_onClick(self):
self.statusBar().showMessage("Switched to window 2")
if __name__ == '__main__':
app=QApplication(sys.argv)
ex=Window()
sys.exit(app.exec_())
【问题讨论】:
窗户是什么? 【参考方案1】:试试看:
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.title = "App"
self.top = 100
self.left = 100
self.width = 680
self.height = 500
self.InitUI()
def InitUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
buttonWindow1 = QPushButton('Window1', self)
buttonWindow1.move(100, 100)
buttonWindow1.clicked.connect(self.buttonWindow1_onClick)
self.lineEdit1 = QLineEdit("Type here what you want to transfer for [Window1].", self)
self.lineEdit1.setGeometry(250, 100, 400, 30)
buttonWindow2 = QPushButton('Window2', self)
buttonWindow2.move(100, 200)
buttonWindow2.clicked.connect(self.buttonWindow2_onClick)
self.lineEdit2 = QLineEdit("Type here what you want to transfer for [Window2].", self)
self.lineEdit2.setGeometry(250, 200, 400, 30)
self.show()
@pyqtSlot()
def buttonWindow1_onClick(self):
self.statusBar().showMessage("Switched to window 1")
self.cams = Window1(self.lineEdit1.text())
self.cams.show()
self.close()
@pyqtSlot()
def buttonWindow2_onClick(self):
self.statusBar().showMessage("Switched to window 2")
self.cams = Window2(self.lineEdit2.text())
self.cams.show()
self.close()
class Window1(QDialog):
def __init__(self, value, parent=None):
super().__init__(parent)
self.setWindowTitle('Window1')
self.setWindowIcon(self.style().standardIcon(QStyle.SP_FileDialogInfoView))
label1 = QLabel(value)
self.button = QPushButton()
self.button.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Expanding)
self.button.setIcon(self.style().standardIcon(QStyle.SP_ArrowLeft))
self.button.setIconSize(QSize(200, 200))
layoutV = QVBoxLayout()
self.pushButton = QPushButton(self)
self.pushButton.setStyleSheet('background-color: rgb(0,0,255); color: #fff')
self.pushButton.setText('Click me!')
self.pushButton.clicked.connect(self.goMainWindow)
layoutV.addWidget(self.pushButton)
layoutH = QHBoxLayout()
layoutH.addWidget(label1)
layoutH.addWidget(self.button)
layoutV.addLayout(layoutH)
self.setLayout(layoutV)
def goMainWindow(self):
self.cams = Window()
self.cams.show()
self.close()
class Window2(QDialog):
def __init__(self, value, parent=None):
super().__init__(parent)
self.setWindowTitle('Window2')
self.setWindowIcon(self.style().standardIcon(QStyle.SP_FileDialogInfoView))
label1 = QLabel(value)
self.button = QPushButton()
self.button.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Expanding)
self.button.setIcon(self.style().standardIcon(QStyle.SP_ArrowLeft))
self.button.setIconSize(QSize(200, 200))
layoutV = QVBoxLayout()
self.pushButton = QPushButton(self)
self.pushButton.setStyleSheet('background-color: rgb(0,0,255); color: #fff')
self.pushButton.setText('Click me!')
self.pushButton.clicked.connect(self.goMainWindow)
layoutV.addWidget(self.pushButton)
layoutH = QHBoxLayout()
layoutH.addWidget(label1)
layoutH.addWidget(self.button)
layoutV.addLayout(layoutH)
self.setLayout(layoutV)
def goMainWindow(self):
self.cams = Window()
self.cams.show()
self.close()
if __name__ == '__main__':
app=QApplication(sys.argv)
ex=Window()
sys.exit(app.exec_())
【讨论】:
对不起,我没有正确描述它。我的意思是,我可以做两个单独的窗口(一起运行,就像你的想法)但我总是想要一个窗口。【参考方案2】:试试这个:
from file2 import Ui_Dialog2 #------>import the class for next window
def buttonWindow1_onClick(self):
self.window=QtWidgets.QMainWindow()
self.ui=Ui_Dialog2() #------------->creating an object
self.ui.setupUi(self.window)
self.window.show()
这里的 Ui_Dialog2() 表示您正在为新窗口创建一个对象(在您的情况下为 Window1 创建对象以将 MainWindow 切换到 Window1 )。所以当您单击下一个按钮调用此函数,以便为下一个窗口创建一个对象并打开该窗口。
【讨论】:
下一个窗口仍然打开。我想要一个窗口,但每个瓷砖都有不同的类别。示例文件树:main.py one.py two.py three.py。然后,如果我在主窗口中按“一个”按钮,屏幕将切换到下一个窗口(不是同时两个窗口)。 不可以创建4个类,然后在main.py的button方法中调用吗?以上是关于PyQt5 窗口间切换的主要内容,如果未能解决你的问题,请参考以下文章
PyQt5 QTabWidget:如何在类和同一窗口中包含的选项卡之间切换?
Python 小白从零开始 PyQt5 项目实战窗口切换的堆叠布局