python3 PyQt4:如何切换QSTackedWidgets,使用QTDesigner构建

时间:2018-02-01 15:27:58

标签: python pyqt4

我要构建一个GUI,它应该有serval页面。可以使用不同的窗口,但更改菜单栏f.e.花费很多时间。这就是为什么我在寻找包括不同的.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>

我的错误在哪里? 谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

The main problem in your code is that you are changing the index to an empty QStackedWidget, if you analyze your code you are creating a new QStackedWidget so currenIndex will always be -1. In addition, it is not appropriate to use sleep() in a GUI. Another error is that you must pass self to loadUi() to load directly to the main Widget.

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_())