如何使用pyqt、qt designer弹出新的window
How to pop up new window using pyqt, qt designer
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4 import uic
MainWindow = uic.loadUiType("simulator_ver_0.1.ui")[0]
AddFuseWindow = uic.loadUiType("simulator_add_fuse_ver_0.1.ui")[0]
AddLoadWindow = uic.loadUiType("simulator_add_load_ver_0.1.ui")[0]
class MyWindow2(QWidget, AddFuseWindow):
def _init_():
super()._init_(self)
self.setupUi(self)
class MyWindow3(QWidget , AddLoadWindow):
def _init_():
super()._init_(self)
self.setupUi(self)
class MyWindow(QMainWindow, MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.connect(self.actionFuse, SIGNAL("triggered()"), self.add_fuse)
self.connect(self.actionLoad, SIGNAL("triggered()"), self.add_load)
def add_fuse(self):
self.w2 = MyWindow2(self)
self.w2.show()
def add_load(self):
self.w3=MyWindow3()
self.w3.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
这是我的 python 代码。
我从 qt designer 加载了三个 ui 文件。
我已经创建了 MyWindow class,它是 MainWindow。我想弹出 MyWindow2 或 MyWindow3,当我点击工具栏时。成功弹出 new window ,但不是我创建的。它只是空的 window。
似乎 python 没有加载 AddFuseWindow 和 AddLoadWindow。
enter image description here
您可以在函数中定义新的弹出窗口 window 并将其连接到工具栏项。
self.toolbaritem.triggered.connect(self.doSomething)
def doSomething(self):
window = OtherWindow()
window.show()
self.close()
class OtherWindow():
def dothis(self):
window = SampleWin()
window.show()
self.close()
您也可以使用 QStackedWidget 来做同样的事情。
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4 import uic
MainWindow = uic.loadUiType("simulator_ver_0.1.ui")[0]
AddFuseWindow = uic.loadUiType("simulator_add_fuse_ver_0.1.ui")[0]
AddLoadWindow = uic.loadUiType("simulator_add_load_ver_0.1.ui")[0]
class MyWindow2(QWidget, AddFuseWindow):
def _init_():
super()._init_(self)
self.setupUi(self)
class MyWindow3(QWidget , AddLoadWindow):
def _init_():
super()._init_(self)
self.setupUi(self)
class MyWindow(QMainWindow, MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.connect(self.actionFuse, SIGNAL("triggered()"), self.add_fuse)
self.connect(self.actionLoad, SIGNAL("triggered()"), self.add_load)
def add_fuse(self):
self.w2 = MyWindow2(self)
self.w2.show()
def add_load(self):
self.w3=MyWindow3()
self.w3.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
这是我的 python 代码。 我从 qt designer 加载了三个 ui 文件。 我已经创建了 MyWindow class,它是 MainWindow。我想弹出 MyWindow2 或 MyWindow3,当我点击工具栏时。成功弹出 new window ,但不是我创建的。它只是空的 window。 似乎 python 没有加载 AddFuseWindow 和 AddLoadWindow。 enter image description here
您可以在函数中定义新的弹出窗口 window 并将其连接到工具栏项。
self.toolbaritem.triggered.connect(self.doSomething)
def doSomething(self):
window = OtherWindow()
window.show()
self.close()
class OtherWindow():
def dothis(self):
window = SampleWin()
window.show()
self.close()
您也可以使用 QStackedWidget 来做同样的事情。