【发布时间】: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_())
【问题讨论】: