python - 在 tkinter 中使用 Askopenfilename 对话框时,键绑定(bind)不起作用

标签 python tkinter key-bindings

我正在开发一个简单的应用程序,用于使用 python 3.4 和 tkinter 读取和显示 zip 文件中的图像文件序列,就像您可能用来读取 .cbz 漫画书文件一样。理想情况下,我想绑定(bind)左键和右键以分别显示最后一张和下一张图像。如果我在代码中指定 zip 文件的名称,则效果很好;但是,如果我使用 filedialog.askopenfilename() 对话框来指定文件,则键盘按键绑定(bind)将不再起作用。

我认为这是由于焦点问题造成的,并且我尝试将焦点设置为绑定(bind)键的标签(均使用 label.focus_set() 方法和askopenfilename() 对话框的父选项)但没有成功。

代码如下。对此的任何帮助将不胜感激,因为它开始让我发疯。

from tkinter import *
from tkinter import filedialog
import io
from PIL import Image, ImageTk
import zipfile

class ComicDisplay():
    def __init__(self, master):
        frame = Frame(master)
        frame.pack(fill='both', expand=1)
        self.parent = master
        self.fname = ""
        self.label = Label(frame, bg="brown", height=500)
        self.current_zip_file = filedialog.askopenfilename(filetypes=[(zip, "*.zip")])
        # self.current_zip_file = "C:\\Users\\Alexis\\Dropbox\\Photos.zip"
        self.image_list = self.acquire_image_list(self.current_zip_file)
        self.current_image_number = 0
        self.pil_image = self.acquire_image(self.current_zip_file, self.image_list[self.current_image_number])
        self.tk_image = ImageTk.PhotoImage(self.pil_image)
        self.parent.title(self.fname)

        self.label.configure(image=self.tk_image)
        self.label.focus_set()
        self.label.bind("<Configure>", self.image_resizing)
        self.label.bind("<Left>", self.get_last_image)
        self.label.bind("<Right>", self.get_next_image)
        self.label.bind("<Button-1>", self.get_next_image)
        self.label.pack(padx=5, pady=5, fill='both', expand=1)

    def acquire_image_list(self, zip_file):
        image_list = []
        with zipfile.ZipFile(zip_file, "r") as myFile:
            for filename in myFile.namelist():
                image_list.append(filename)
        image_list.sort()
        return image_list

    def acquire_image(self, zip_file, image_file):
        with zipfile.ZipFile(zip_file, "r") as myFile:
            self.fname = image_file
            image_bytes = myFile.read(image_file)
            data_stream = io.BytesIO(image_bytes)
            pil_image = Image.open(data_stream)
            pil_image = self.image_sizer(pil_image)
            return pil_image

    def image_sizer(self, image_file, window_size=500):
        w, h = image_file.size
        if w > h:
            image_file_height = int(h*(window_size/w))
            image_file = image_file.resize((window_size, image_file_height), Image.ANTIALIAS)
        else:
            image_file_width = int(w*(window_size/h))
            image_file = image_file.resize((image_file_width, window_size), Image.ANTIALIAS)
        return image_file

    def image_resizing(self, event):
        new_height = root.winfo_height() - 14
        new_size_image = self.image_sizer(self.pil_image, new_height)
        self.tk_image = ImageTk.PhotoImage(new_size_image)
        self.label.configure(image=self.tk_image)

    def get_next_image(self, event):
        if self.current_image_number >= len(self.image_list)-1:
            self.current_image_number = 0
        else:
            self.current_image_number += 1
        self.update_image()

    def get_last_image(self, event):
        if self.current_image_number == 0:
            self.current_image_number = len(self.image_list)-1
        else:
            self.current_image_number -= 1
        self.update_image()

    def update_image(self):
        self.fname = self.image_list[self.current_image_number]
        self.pil_image = self.acquire_image(self.current_zip_file, self.image_list[self.current_image_number])
        self.tk_image = ImageTk.PhotoImage(self.pil_image)
        self.parent.title(self.fname)
        self.image_resizing(None)




root = Tk()
app = ComicDisplay(root)
root.mainloop()

最佳答案

Bryan 的评论给出了答案:将打开文件对话延迟到窗口初始化之后即可解决问题。创建文件打开方法可以让键绑定(bind)正常工作,而不是在应用程序启动时打开文件。

关于python - 在 tkinter 中使用 Askopenfilename 对话框时,键绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35372230/

相关文章:

python - 如何从 SqlAlchemy 创建和恢复备份?

Python 速度优化

python - 如何在 Python Tkinter GUI 中的 'label' 小部件中显示更新的值?

python - 将分数转换为 float ?

python - 有没有办法在 Redshift 中使用 CONCAT(table_name(col1, col2, col3,.....)) 连接可变数量的列?

wpf - 如何在WPF中为反斜杠键创建键绑定(bind)?

javascript - 使用 jQuery 将箭头键绑定(bind)到导航

java - 禁用 Enter 键在 JTable 中向下移动一行

python - Scrapy:循环搜索结果仅返回第一项

python - 增加 tkSimpleDialog 窗口大小