tkinter-我正在尝试禁用前进/后退按钮,但似乎不起作用

时间:2020-03-19 22:47:35

标签: python tkinter

我正在尝试构建一个图像查看器,以从文件夹中加载图像。它应该具有前进/后退和退出按钮。似乎可以解决一个问题:

所以我只是用这个来拍摄图像路径:

def get_files_from_folder(path, allowed_extensions):
    found_paths = []

    for (dir_path, _, file_names) in os.walk(path):
        for file_name in file_names:
            for allowed_extension in allowed_extensions:
                if file_name.lower().endswith(allowed_extension.lower()):
                    found_paths.append(os.path.join(dir_path, file_name))

                    break

    return found_paths

我有用于图像查看器的tkinter UI:

class UI:
    def __init__(self, icon_path, images_folder_path):
        self.current_index = 0
        self.root = tk.Tk()
        self.root.title('Images')
        self.root.iconbitmap(icon_path)

        self.button_quit = tk.Button(self.root, text = 'Quit', padx = 60, command = self.root.quit)
        self.button_forward = tk.Button(self.root, text = '>>', command = self.forward)
        self.button_backward = tk.Button(self.root, text = '<<', command = self.backward)

        self.button_quit.grid(row = 1, column = 1)
        self.button_forward.grid(row = 1, column = 2)
        self.button_backward.grid(row = 1, column = 0)

        self.images_paths = get_files_from_folder(images_folder_path, ['.jpg', '.png'])
        self.tk_images = []
        print(get_files_from_folder)
        for image_path in self.images_paths:
            self.tk_images.append(ImageTk.PhotoImage(Image.open(image_path)))

        self.current_image = tk.Label(image = self.tk_images[0])
        self.current_image.grid(column = 0, row = 0, columnspan = 3)

        self.root.mainloop()

由于某种原因,在这里,当我使用tk.DISABLED时,它不会禁用它

    def backward(self):

        if self.current_index == 0:
            self.button_backward = self.button_backward = tk.Button(self.root, text = '<<', command = self.backward, state = tk.DISABLED)

        self.current_image.grid_forget()

        self.current_index -= 1
        self.current_image = tk.Label(image = self.tk_images[self.current_index])
        self.current_image.grid(column = 0, row = 0, columnspan = 3)

与前锋相同:

    def forward(self):

        self.current_image.grid_forget()

        if self.current_index == len(self.tk_images)-1:
            self.button_forward = self.button_forward = tk.Button(self.root, text = '>>', command = self.forward, state = tk.DISABLED)
        else:
            self.button_forward.state = tk.ACTIVE
        self.current_index += 1
        self.current_image = tk.Label(image = self.tk_images[self.current_index])
        self.current_image.grid(column = 0, row = 0, columnspan = 3)

1 个答案:

答案 0 :(得分:0)

关于向前和向后Button的命令,您当前的代码至少有几处​​错误。至于停用Button,可以通过调用它们的config()方法来实现-通过创建一个新方法或为现有值{{1 }}属性(即state)。

类似地,每次按下其中一个按钮且图像上的图像不断变化时,连续创建新的self.button_forward.state = tk.ACTIVE小部件也不是一个好习惯。最好更改现有tk.Label的{​​{1}}选项。这样做通常还简化了需要完成的工作。

此外,钳位image=的逻辑存在缺陷,将使它超出范围而发生Label。这比我预期的要复杂,但是我认为我找到了一种处理它的好方法,即将所有逻辑置于私有方法self.current_index中,并通过向前和向后回调函数进行调用。

最后,用户@ acw1668评论说,它在程序启动时将所有图像加载到内存中可能会很慢。为避免这种情况,我用对另一个名为IndexError的新私有方法的调用替换了您拥有的所有已加载图像(_change_current_index())的列表,该方法通过应用self.tk_images装饰器来缓存其结果对此。这样可以使该方法记住有限数量的索引已经返回的值,从而可以随时限制它们在内存中的数量。

请注意,我还重新格式化了代码,使其更好地符合PEP 8 - Style Guide for Python Code准则,以使其更具可读性。

_open_image()