【问题标题】:How to go through next and previous list items with buttons?如何使用按钮浏览下一个和上一个列表项?
【发布时间】:2021-07-16 08:53:26
【问题描述】:

我有一个清单:

files = ["image1.png", "image2.png", "image3.png", "image4.png", "image5.png", "image6.png"]

还有 PyQt5 按钮和一个标签:

我怎样才能将列表的第一项放在标签上,然后逐个浏览列表:

下一步按钮单击、image2.png、单击、image3.png、单击、image4. png...)

上一个按钮单击、image3.png、单击、image2.png、单击、image1. png...)

并且标签应该相应地更新。

我已经尝试了一整天并寻找答案,但无法让它发挥作用。 This 可能会有所帮助,不过我的技能不足以利用这些建议。

创建上述按钮窗口的所有代码:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

class App(QWidget):

    def __init__(self):
        super().__init__()
        self._files = deque()
        self._filesCount = len(self._files)
        self._setupUI()
        self._connectSignalsSlots()

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 button - pythonspot.com'
        self.left = 10
        self.top = 10
        self.width = 320
        self.height = 200
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        button = QPushButton('Print Files List', self)
        button.move(100,70)
        button.clicked.connect(self.loadFiles)

        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        button = QPushButton('Print Next Filename', self)
        button.move(160,110)
        button.clicked.connect(self.nextFilename)
        label = QLabel('image1.png (should be first of the list, \n and update with button presses)', self)
        label.move(35,140)

        button = QPushButton('Print Previous Filename', self)
        button.move(10,110)
        button.clicked.connect(self.previousFilename)

        self.show()

    @pyqtSlot()
    def loadFiles(self):
        files = ["image1.png", "image2.png", "image3.png", "image4.png", "image5.png", "image6.png"]
        if len(files) > 0:
            for file in files:
                print(file)

    def nextFilename(self):
        print('nextFilenameButton click')

    def previousFilename(self):
        print('previousFilenameButton click')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

【问题讨论】:

    标签: python pyqt5


    【解决方案1】:

    解决方案是处理用于获取文件名的索引(或光标)。并且该索引必须通过按钮修改

    import sys
    
    from PyQt5.QtGui import QPixmap
    from PyQt5.QtWidgets import QApplication, QGridLayout, QLabel, QPushButton, QWidget
    
    
    class Widget(QWidget):
        def __init__(self, parent=None):
            super().__init__(parent)
            self._current_index = 0
            self._filenames = []
    
            self.previous_button = QPushButton("Previous")
            self.next_button = QPushButton("Next")
            self.print_button = QPushButton("Print")
            self.label = QLabel()
    
            lay = QGridLayout(self)
            lay.addWidget(self.previous_button, 0, 0)
            lay.addWidget(self.next_button, 0, 1)
            lay.addWidget(self.print_button, 0, 2)
            lay.addWidget(self.label, 1, 0, 1, 3)
    
            self.previous_button.clicked.connect(self.handle_previous)
            self.next_button.clicked.connect(self.handle_next)
            self.print_button.clicked.connect(self.handle_print)
    
            self._update_button_status(False, True)
    
            self.load_files()
    
        def load_files(self):
            self._filenames = [
                "image1.png",
                "image2.png",
                "image3.png",
                "image4.png",
                "image5.png",
                "image6.png",
            ]
            self.current_index = 0
    
        def handle_next(self):
            self.current_index += 1
    
        def handle_previous(self):
            self.current_index -= 1
    
        def handle_print(self):
            for filename in self._filenames:
                print(filename)
    
        @property
        def current_index(self):
            return self._current_index
    
        @current_index.setter
        def current_index(self, index):
            if index <= 0:
                self._update_button_status(False, True)
            elif index >= (len(self._filenames) - 1):
                self._update_button_status(True, False)
            else:
                self._update_button_status(True, True)
    
            if 0 <= index < len(self._filenames):
                self._current_index = index
                filename = self._filenames[self._current_index]
                pixmap = QPixmap(filename)
                self.label.setPixmap(pixmap)
    
        def _update_button_status(self, previous_enable, next_enable):
            self.previous_button.setEnabled(previous_enable)
            self.next_button.setEnabled(next_enable)
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        w = Widget()
        w.resize(640, 480)
        w.show()
        sys.exit(app.exec_())
    

    注意:拥有 2 个同名方法是没有用的,因为在 python 中只会使用最后一个实现。

    【讨论】:

    • 哎呀,当我压缩代码时,同名的东西溜进来了。但是非常感谢!这比我希望的要好! :) 但是,我需要输入什么来获取标签文本的当前文件名?
    • 啊,终于意识到了!制作一个新标签并在self.label.setPixmap(pixmap) 行下方添加self.label2.setText(filename) :)
    • @ManuJärvinen 我的例子很简单,我不打算实现很多功能。请避免添加不与我的答案争论的 cmets。
    • 对不起 :( 只是想着想继续下一步的人。谢谢你的回答。(好的,没有更多的 cmets)
    • @ManuJärvinen 在这里,我们不帮助实施项目(因为您的 cmets 似乎会这样做)但为了解决特定问题,我们不打算涵盖太多,而是最低限度和必要的因为这样我们就可以获得高质量的问答集。请阅读How to Ask 并查看tour
    猜你喜欢
    • 2014-11-15
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多