[英]python3 PyQt4: How to toggle QStackedWidgets, built with QTDesigner
我將構建一個小GUI,其中應該包含服務頁面。 可以使用不同的窗口,但是更改菜單欄會花費很多時間。 那就是為什么我要搜索包含不同的.ui的原因。 但是然后兩個* .ui在哪里重疊,我無法隱藏“舊” .ui
現在,我嘗試使用QStackedWidgets,因為它們似乎非常適合我的情況(切換內容)。
但是我很困惑如何在這些頁面之間切換...我用QtDesigner構建了一個* .ui。 現在,我加載該* .ui,並希望在頁面之間切換。
首先,我嘗試自動切換,但不起作用? 不知道為什么...
import sys
from PyQt4 import QtGui
from PyQt4.uic import loadUi
try:
from GUI.UI.MainWindow_UI import Ui_MainWindow
except ImportError:
from PyQt4.uic import loadUi
Ui_MainWindow = None
from PyQt4.uic import compileUiDir
compileUiDir('GUI/')
from time import sleep
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
super(MainWindow, self).__init__(parent)
self.ui = loadUi('GUI/gui.ui')
self.ui.show()
self.stackedWidget = QtGui.QStackedWidget(self)
self.stackedWidget.setCurrentIndex(1)
sleep(1)
print(self.stackedWidget.currentIndex())
self.stackedWidget.setCurrentIndex(0)
sleep(1)
print(self.stackedWidget.currentIndex())
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = MainWindow()
#gui.setParent(QtGui.QApplication.activeWindow())
sys.exit(app.exec_())
兩次打印輸出均為-1。
GUI / gui.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>424</width>
<height>282</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QStackedWidget" name="stackedWidget">
<property name="geometry">
<rect>
<x>30</x>
<y>20</y>
<width>341</width>
<height>211</height>
</rect>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="page">
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>90</x>
<y>60</y>
<width>57</width>
<height>14</height>
</rect>
</property>
<property name="text">
<string>Page 1</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>50</x>
<y>130</y>
<width>191</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>toggle to page 1</string>
</property>
</widget>
</widget>
<widget class="QWidget" name="page_2">
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>120</x>
<y>60</y>
<width>57</width>
<height>14</height>
</rect>
</property>
<property name="text">
<string>page 2</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>90</x>
<y>120</y>
<width>201</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>toggle to page 1</string>
</property>
</widget>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>424</width>
<height>19</height>
</rect>
</property>
<widget class="QMenu" name="menuInfo">
<property name="title">
<string>Info</string>
</property>
<addaction name="actionHelp"/>
<addaction name="actionExit"/>
</widget>
<addaction name="menuInfo"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionHelp">
<property name="text">
<string>Help</string>
</property>
</action>
<action name="actionExit">
<property name="text">
<string>Exit</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>
我的錯誤在哪里? 謝謝你的幫助!
代碼中的主要問題是,您正在將索引更改為空的QStackedWidget
,如果您分析代碼,則將創建一個新的QStackedWidget
因此currenIndex
始終為-1
。 另外,在GUI中使用sleep()
是不合適的。 另一個錯誤是您必須將self
傳遞給loadUi()
才能直接加載到主loadUi()
小部件。
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.uic import loadUi
try:
from GUI.UI.MainWindow_UI import Ui_MainWindow
except ImportError:
from PyQt4.uic import loadUi
Ui_MainWindow = None
from PyQt4.uic import compileUiDir
compileUiDir('GUI/')
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
loadUi('GUI/gui.ui', self)
self.show()
self.stackedWidget.setCurrentIndex(1)
loop = QtCore.QEventLoop()
QtCore.QTimer.singleShot(1000, loop.quit)
loop.exec_()
print(self.stackedWidget.currentIndex())
self.stackedWidget.setCurrentIndex(0)
loop = QtCore.QEventLoop()
QtCore.QTimer.singleShot(1000, loop.quit)
loop.exec_()
print(self.stackedWidget.currentIndex())
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = MainWindow()
#gui.setParent(QtGui.QApplication.activeWindow())
sys.exit(app.exec_())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.