Skip to main content
 首页 » 编程设计

Python之Tkinter之通过双击鼠标切换/退出全屏图像

2025年02月15日6dudu

我正在尝试编写将通过远程终端连接执行的代码。首先它会提示要使用的图像文件,然后全屏显示所述图像。我能够拼凑代码来实现这一目标。唯一的问题是我无法在不退出终端进程的情况下转义全屏图像。

我已经尽我所能地查看了所有地方,但似乎无法解决这种特殊情况。任何帮助将不胜感激,如果需要,我很乐意提供更多信息。 (我使用 Python 2.7 只是因为我引用的初始代码是 2.7)。

这是我目前所拥有的:

#!/usr/bin/env python 
 
from Tkinter import * 
from PIL import Image, ImageTk 
 
def choose_picture(): 
    user_input = raw_input("Choose picture file: ") 
    user_input = (user_input + ".jpg") 
 
    root = Tk() 
    main_image = Image.open(user_input)  # Room label image 
    photo = ImageTk.PhotoImage(main_image) 
    Label(root, image=photo).pack() 
    root.attributes('-fullscreen', True) 
    root.mainloop() 
 
choose_picture() 

请您参考如下方法:

感谢您的帮助。最后,这是对我所引用的资源进行更多试验和错误的问题。包括我的后代最终代码,尽管我确信有更好的方法来做到这一点。

#!/usr/bin/env python 
 
from Tkinter import * 
from PIL import Image, ImageTk 
 
"""Asks for picture file. Loads picture file with '.jpg' appended to name entered. 
Double Click and F11 both resume fullscreen. 
Triple Click and Escape both close fullscreen.""" 
 
def choose_picture(): 
    user_input = raw_input("Choose picture file: ") 
    user_input = (user_input + ".jpg") 
 
    root = Tk() 
    root.state = True 
    root.attributes("-fullscreen", True) 
 
    main_image = Image.open(user_input)  # Room label image 
    photo = ImageTk.PhotoImage(main_image) 
    Label(root, image=photo).pack() 
 
    def resume_fullscreen(self, event=None): 
        root.state = True 
        root.attributes("-fullscreen", True) 
        return "break" 
 
    def end_fullscreen(self, event=None): 
        root.state = False 
        root.attributes("-fullscreen", False) 
        root.geometry("200x200") 
        return "break" 
 
    root.bind("<Double-1>", resume_fullscreen) 
    root.bind("<Triple-1>", end_fullscreen) 
    root.bind("<F11>", resume_fullscreen) 
    root.bind("<Escape>", end_fullscreen) 
    root.mainloop() 
 
choose_picture()