繁体   English   中英

我不知道为什么这段简单的 python 代码没有运行

[英]I don't know why this simple piece of python code isn't running

我在这段代码中的想法是使用 Tkinter 运行一个应用程序,该应用程序根据我在键盘上按下的数字“点亮”七段显示器。

import tkinter as tk
import keyboard
import time
from PIL import ImageTk, Image

def main():
    window = tk.Tk()
    window.title("AutoSegment")
    window.geometry("459x767")
    path=r"C:\Users\The Man Himself\Desktop\SSG\welcome.jpg"
    img = ImageTk.PhotoImage(Image.open(path))
    panel = tk.Label(window, image = img).pack(side = "bottom", fill = "both", expand = "yes")
    listener()
    tk.mainloop()

def set(name):
    path=r"C:\Users\The Man Himself\Desktop\SSG\%s.jpg" %name
    img = ImageTk.PhotoImage(Image.open(path))
    panel = tk.Label(window, image = img).pack(side = "bottom", fill = "both", expand = "yes")
    listener()
    tk.mainloop()

def listener():
    while True:
        try:
            if keyboard.is_pressed('1'):
                set("1")
                break
            elif keyboard.is_pressed('2'):
                set("2")
                break
            elif keyboard.is_pressed('3'):
                set("3")
                break
            elif keyboard.is_pressed('4'):
                set("4")
                break
            elif keyboard.is_pressed('5'):
                set("5")
                break
            elif keyboard.is_pressed('6'):
                set("6")
                break
            elif keyboard.is_pressed('7'):
                set("7")
                break
            elif keyboard.is_pressed('8'):
                set("8")
                break
            elif keyboard.is_pressed('9'):
                set("9")
                break
            elif keyboard.is_pressed('0'):
                set("0")
                break
        except:
            set("error")

main()

我没有使用过keyboard模块,但我可以向您展示如何在没有它的情况下工作。

几件事; window 是在函数内部创建的,这意味着名称window是该函数的本地名称。 而是在全局范围内创建窗口。 此外,函数set()是一个内置函数,如果您重新定义它,您将无法访问内置函数。 我把它叫做set_display()

由于您将在panel更改图像,因此最好在全局命名空间中创建它。 此外,为了能够更改它,您必须保留一个参考,即给它命名panel ,然后打包。 否则,名称panel将指向pack()的返回值,即 = None

当您稍后在函数set_display()更改标签中的图像时,您还必须在标签中保存对图像的引用,在我的示例代码中明确注释。

然后我使用bind()来挂钩键盘,这是 tkinter 小部件中的标准方法。 之后我启动mainloop() ,它等待直到按下一个键,然后调用keypress()

import tkinter as tk
from PIL import ImageTk, Image

def set_display(name):
    path = r"C:\Users\The Man Himself\Desktop\SSG\%s.jpg" %name
    img = ImageTk.PhotoImage(Image.open(path))
    panel.config(image=img) # Load new image into label
    panel.image = img       # Save reference to image

def keypress(event):
    if event.char == '':    # Shift, ctrl etc, returns empty char
        set_display('error')
    elif event.char in '1234567890':    # Hook all numbers
        set_display(event.char)
    else:
        set_display('error')

window = tk.Tk()
window.title("AutoSegment")
window.geometry("459x767")

# Create Seven Segment Display label in global namespace
path = r"C:\Users\The Man Himself\Desktop\SSG\welcome.jpg"
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(window, image=img)
panel.pack(side="bottom", fill="both", expand="yes")

window.bind('<KeyPress>', keypress)
window.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM