【问题标题】:python3 PyQt4: How to toggle QStackedWidgets, built with QTDesignerpython3 PyQt4:如何切换 QStackedWidgets,使用 QTDesigner 构建
【发布时间】:2018-02-01 15:27:57
【问题描述】:

我将构建一个小 GUI,它应该有多个页面。可以使用不同的窗口,但更改菜单栏 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>

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

【问题讨论】:

    标签: python pyqt4


    【解决方案1】:

    代码中的主要问题是您将索引更改为空的QStackedWidget,如果您分析代码,您将创建一个新的QStackedWidget,因此currenIndex 将始终为-1。此外,在 GUI 中使用 sleep() 是不合适的。另一个错误是您必须将self 传递给loadUi() 才能直接加载到主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_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-20
      相关资源
      最近更新 更多