【问题标题】:Rename a item on a list and go to another item with next and prev buttons, but keep the renamed items in the list重命名列表中的项目并使用下一个和上一个按钮转到另一个项目,但将重命名的项目保留在列表中
【发布时间】:2021-07-16 14:10:59
【问题描述】:

我有一个清单:

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

还有一个新的所需文件名:

def createNewFilename(self):
    self._newFilename = "New filename"

还有 PyQt5 按钮和一个标签:

如果我:

  • 下一步进入image2.png
  • 重命名按钮

那么:如何将列表项 image2.png 重命名为 New filename 并保留在列表中并更新到标签中?

  • 当我一直按 PreviousNext 按钮时,我也应该能够重命名其他文件名,并通过单击浏览列表中更新的文件名。

这是整个事情的简短代码:

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._newFilename = ""
        self._createNewFilename = ""

        self.previous_button = QPushButton("Previous")
        self.next_button = QPushButton("Next")
        self.rename_button = QPushButton("Rename")
        self.label = QLabel()
        self.label2 = QLabel()

        lay = QGridLayout(self)
        lay.addWidget(self.previous_button, 0, 0)
        lay.addWidget(self.next_button, 0, 1)
        lay.addWidget(self.rename_button, 0, 2)
        lay.addWidget(self.label2, 1, 0, 1, 3)
        lay.addWidget(self.label, 2, 0, 1, 3)

        self.previous_button.clicked.connect(self.handle_previous)
        self.next_button.clicked.connect(self.handle_next)
        self.rename_button.clicked.connect(self.handle_rename)

        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
    @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)
            self.label2.setText(filename)

    def _update_button_status(self, previous_enable, next_enable):
        self.previous_button.setEnabled(previous_enable)
        self.next_button.setEnabled(next_enable)

    def createNewFilename(self):
        self._newFilename = "New filename"

    def handle_rename(self, index):
        newFilename = self._newFilename
        print(newFilename) 

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = Widget()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

【问题讨论】:

  • 您的问题不清楚。什么时候应该重命名第二项?作为对什么的回应?
  • @musicamante 好的。现在怎么样,我编辑了一下。
  • 您永远不会在“hadle_rename”功能中更改列表中的任何内容 - 那么为什么要进行任何更改?
  • 您需要将self._filenames[self._current_index] 中的项目替换为您的新名称...直到您在列表中不做任何更改!
  • 为什么不在handle_rename 中添加self._filenames[self.current_index] = newFilenameself.label2.setText(newFilename)

标签: python python-3.x pyqt5


【解决方案1】:

你的问题不清楚。 您是要重命名计算上的实际文件还是仅重命名 python 中的列表?

最后,你可以这样做:

    def handle_rename(self):
        self.createNewFilename()
        self._filenames[self._current_index] = self._newFilename
        self.current_index = self._current_index

首先,假设self._filenames 包含路径,您可以这样做:

import os

    def handle_rename(self):
        self.createNewFilename()
        try:
            os.rename(self._filenames[self._current_index], self._newFilename)
        except OSError:
            pass
        else:
            self._filenames[self._current_index] = self._newFilename
            self.current_index = self._current_index

如果新文件名不是路径,则最后一段代码需要稍作修改。

【讨论】:

  • 最终是的,在我的电脑上 - 而且在标签中显示当前和更改的文件名。我想我可以处理计算机部分的重命名。
  • 如果是这样,只需输入第一个 handle_rename 代码就可以了!
  • 哦,天哪,由于某种原因,如果 createNewFilename() 更复杂,这将停止工作,非常奇怪.... - images.manujarvinen.com/python_problem.png - 即使我保持 handle_rename 相同:/ 可以是什么原因?
  • 能否以文本格式提供新的 createNewFilename() 代码?
  • 你需要删除return newFilename这一行
猜你喜欢
  • 2017-09-28
  • 2021-11-23
  • 1970-01-01
  • 2021-12-22
  • 2021-03-17
  • 1970-01-01
  • 1970-01-01
  • 2015-06-04
  • 2022-07-18
相关资源
最近更新 更多