QStackedWidget 的 setCurrentIndex 来自其外部 class

setCurrentIndex of QStackedWidget from outside its class

我正在尝试从 class 外部设置 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_())

单击按钮时出现错误:

类型对象'ut_Screen1'没有属性'StackBot'

提前感谢您的指导。

我认为您对 OOP 的了解很差,所以我建议您复习一下。

一个class是一个抽象的概念,它只对使用class创建的对象的行为进行建模,所以ut_Screen1不是对象而是名称class。只有对象可以使用 class 的非静态方法。总之,你不应该使用 ut_Screen1.

一个可能的解决方案是使用 MainWindow class 的 myWindow 对象访问名为 start_screen 的 ut_Screen1 class 对象:

def ut_ButtonClicked(name):
    try:
        if name == "Button1":
            myWindow.start_screen.StackBot.setCurrentIndex(1)
    except Exception as e:
        print(e)