来自其类外的QStackedWidget的setCurrentIndex
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了来自其类外的QStackedWidget的setCurrentIndex相关的知识,希望对你有一定的参考价值。
我试图从其类外部设置StackedWidget的索引,但得到该对象没有属性的错误。我的代码如下(我为搞砸的缩进道歉,它没有正确粘贴):
import sys
from functools import partial
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import QtCore, QtWidgets, QtGui
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setWindowTitle('Sample')
self.resize(400,400)
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
self.central_widget = QStackedWidget()
self.setCentralWidget(self.central_widget)
self.start_screen = ut_Screen1()
self.central_widget.addWidget(self.start_screen)
self.central_widget.setCurrentWidget(self.start_screen)
class ut_Screen1(QWidget):
def __init__ ( self ):
super ( ut_Screen1, self ).__init__ ()
nav_layout = QVBoxLayout()
nav_layout.setSpacing(0)
nav_layout.setContentsMargins(0,0,0,0)
self.setLayout(nav_layout)
self.frame_top = QFrame()
self.frame_top.setMinimumSize(QtCore.QSize(400,100))
self.frame_top.setMaximumSize(QtCore.QSize(400, 100))
self.frame_top.setStyleSheet("background-color: rgb(100, 100, 255)")
self.frame_top.stack_t1 = ut_Nav()
self.frame_top.StackTop = QStackedWidget(self)
self.frame_top.StackTop.addWidget(self.frame_top.stack_t1)
layout_top_h = QVBoxLayout(self.frame_top)
layout_top_h.addWidget(self.frame_top.StackTop)
self.stack_b1 = ut_Bottom1()
self.stack_b2 = ut_Bottom2()
self.StackBot = QStackedWidget(self)
self.StackBot.addWidget(self.stack_b1)
self.StackBot.addWidget(self.stack_b2)
nav_layout.addWidget(self.frame_top)
nav_layout.addWidget(self.StackBot)
class ut_Bottom1(QWidget):
def __init__(self):
super(ut_Bottom1, self).__init__()
selection_layout = QVBoxLayout()
self.setLayout(selection_layout)
self.frame_bot = QFrame()
selection_layout.addWidget(self.frame_bot)
self.lblBottom1 = QLabel ( 'Bottom1', self.frame_bot )
self.lblBottom1.move ( 50, 50 )
class ut_Bottom2(QWidget):
def __init__(self):
super(ut_Bottom2, self).__init__()
selection_layout = QVBoxLayout()
self.setLayout(selection_layout)
self.frame_bot = QFrame()
selection_layout.addWidget(self.frame_bot)
self.lblBottom1 = QLabel ( 'Bottom2', self.frame_bot )
self.lblBottom1.move ( 50, 50 )
class ut_Nav(QWidget):
clicked = pyqtSignal ()
def __init__ ( self ):
super ( ut_Nav, self ).__init__ ()
nav_layout = QVBoxLayout()
self.setLayout(nav_layout)
self.frame_nav = QFrame()
nav_layout.addWidget(self.frame_nav)
b1 = QtWidgets.QPushButton ("Button1", self.frame_nav )
b1.setObjectName ('Button1')
b1.clicked.connect (partial(ut_ButtonClicked, b1.objectName()))
def ut_ButtonClicked(name):
try:
if name == "Button1":
ut_Screen1.StackBot.setCurrentIndex(1)
except Exception as e:
print(e)
if __name__ == '__main__':
app = QApplication(sys.argv)
myWindow = MainWindow()
myWindow.show ()
sys.exit(app.exec_())
单击按钮时出现错误:
type对象'ut_Screen1'没有属性'StackBot'
提前感谢任何指导。
答案
我认为你对OOP的了解很差,所以我建议你复习一下。
类是一个抽象概念,它只模拟使用类创建的对象的行为,因此ut_Screen1不是对象而是类的名称。只有对象可以使用该类的非静态方法。总之,你不应该使用ut_Screen1。
一种可能的解决方案是使用MainWindow类的myWindow对象访问名为start_screen的ut_Screen1类的对象:
def ut_ButtonClicked(name):
try:
if name == "Button1":
myWindow.start_screen.StackBot.setCurrentIndex(1)
except Exception as e:
print(e)
以上是关于来自其类外的QStackedWidget的setCurrentIndex的主要内容,如果未能解决你的问题,请参考以下文章