简体   繁体   English

在 PyQt5 中,如何从 QStackedWidget 上的 label 单击按钮时移动到另一个 label?

[英]In PyQt5, how do I move to another label when clicking a button from a label on a QStackedWidget?

In PyQt5, how do I move to another label when clicking a button from a label on a StackedWidget?在 PyQt5 中,如何从 StackedWidget 上的 label 单击按钮时移动到另一个 label?

When you click pushButton_3 , I want the After screen(After label) to appear.当您单击pushButton_3时,我希望出现 After 屏幕(After 标签)。

What code should I use to display the After screen(After label)?我应该使用什么代码来显示 After 屏幕(After 标签)?

        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_3.clicked.connect(self.click_next)


    def click_next(self):
        pass
        ## I want to move to After label

Full Code:完整代码:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class Before(QWidget): ## Before label

    def __init__(self):
        super(Before, self).__init__()
        self.label = QtWidgets.QLabel(self)
        self.label.setGeometry(QtCore.QRect(240, 210, 301, 81))
        font = QtGui.QFont()
        font.setPointSize(30)
        self.label.setFont(font)
        self.label.setText("Before 화면") # 화면 == screen
        self.label.setObjectName("label")

        self.pushButton_3 = QtWidgets.QPushButton(self)
        self.pushButton_3.setGeometry(QtCore.QRect(590, 220, 141, 61))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.pushButton_3.setText("NEXT")
        self.pushButton_3.setFont(font)
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_3.clicked.connect(self.click_next)


    def click_next(self):
        pass
        ## I want to move to After label



class After(QWidget): ## After label

    def __init__(self):
        super(After, self).__init__()
        self.label_2 = QtWidgets.QLabel(self)
        self.label_2.setGeometry(QtCore.QRect(240, 210, 301, 81))
        font = QtGui.QFont()
        font.setPointSize(30)
        self.label_2.setFont(font)
        self.label_2.setText("After 화면") # 화면 == screen
        self.label_2.setObjectName("label_2")

        self.pushButton_4 = QtWidgets.QPushButton(self)
        self.pushButton_4.setGeometry(QtCore.QRect(30, 220, 141, 61))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.pushButton_4.setText("BEFORE")
        self.pushButton_4.setFont(font)
        self.pushButton_4.setObjectName("pushButton_4")
        self.pushButton_4.clicked.connect(self.click_before)

    def click_before(self):
        pass
        ## I want to move to Before label


class Ui_StackedWidget(QWidget):
    def __init__(self):
        QWidget.__init__(self, flags=Qt.Widget)
        self.stk_w = QStackedWidget(self)
        self.setupUi()


   def setupUi(self):
        self.setWindowTitle("Example3-02")
        self.resize(800, 600)

        widget_laytout = QBoxLayout(QBoxLayout.LeftToRight)
        self.stk_w.addWidget(Before())
        self.stk_w.addWidget(After())
        widget_laytout.addWidget(self.stk_w)
        self.setLayout(widget_laytout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = Ui_StackedWidget()
    form.show()
    exit(app.exec_())

This is the Before screen.这是之前的屏幕。 在此处输入图像描述

This is the After screen.这是 After 屏幕。 在此处输入图像描述

You can connect those buttons from the main window to a function that switches the pages of the stacked widget.您可以将这些按钮从主 window 连接到切换堆叠小部件页面的 function。 In order to do so, you should keep a reference to those widgets, instead of creating them within addWidget() .为此,您应该保留对这些小部件的引用,而不是在addWidget()中创建它们。

class Ui_StackedWidget(QWidget):
    def __init__(self):
        QWidget.__init__(self, flags=Qt.Widget)
        self.stk_w = QStackedWidget(self)
        self.setupUi()

   def setupUi(self):
        self.setWindowTitle("Example3-02")
        self.resize(800, 600)

        widget_laytout = QBoxLayout(QBoxLayout.LeftToRight)
        self.before = Before()
        self.stk_w.addWidget(self.before)
        self.after = After()
        self.stk_w.addWidget(self.after)
        widget_laytout.addWidget(self.stk_w)
        self.setLayout(widget_laytout)

        self.before.pushButton_3.clicked.connect(self.goToAfter)
        self.after.pushButton_4.clicked.connect(self.goToBefore)

    def goToAfter(self):
        self.stk_w.setCurrentWidget(self.after)

    def goToBefore(self):
        self.stk_w.setCurrentWidget(self.before)

Note: there's no need to add the flags argument to the __init__ , all QWidgets already have that flag set.注意:不需要将flags参数添加到__init__ ,所有 QWidgets 都已经设置了该标志。 Also, if you're not dealing with right-to-left languages, you can just use create a QHBoxLayout() instead of a QBoxLayout(QBoxLayout.LeftToRight): while the result is the same, it's more readable and the preferred convention.此外,如果您不处理从右到左的语言,您可以只使用创建 QHBoxLayout() 而不是 QBoxLayout(QBoxLayout.LeftToRight):虽然结果相同,但它更具可读性和首选约定。

Some alternative way to other answer if you're planning to use more than 2 widgets:如果您打算使用 2 个以上的小部件,则可以使用其他答案的替代方法:

from PySide2.QtWidgets import QWidget, QApplication, QPushButton, QVBoxLayout, QLabel, QStackedWidget
import sys

# Using PySide2 as an alternative to PyQt5 - Only difference would be import route.


class Base(QWidget):

    def __init__(self, label):
        super().__init__()

        self.label = QLabel(label)
        self.previous_button = QPushButton("Next")

        self.layout = QVBoxLayout()
        self.layout.addWidget(self.label)
        self.layout.addWidget(self.previous_button)

        self.setLayout(self.layout)


class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.widgets = [Base(f"{i}번 화면") for i in range(3)]

        self.stack = QStackedWidget()
        for widget in self.widgets:
            widget.previous_button.released.connect(self.onSignal)
            self.stack.addWidget(widget)

        self.layout = QVBoxLayout()
        self.layout.addWidget(self.stack)

        self.setLayout(self.layout)

    def onSignal(self):
        current_idx = self.stack.currentIndex()
        idx_next = 0 if current_idx == self.stack.count() - 1 else current_idx + 1
        self.stack.setCurrentIndex(idx_next)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

You can cycle thru multiple instances in this case, or add reverse button to cycle both way.在这种情况下,您可以循环通过多个实例,或者添加反向按钮以双向循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM