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

时间:2019-02-02 21:41:42

标签: python tkinter

我在这段代码中的想法是运行一个带有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()

1 个答案:

答案 0 :(得分:0)

我还没有使用keyboard模块,但是我可以向您展示如何不使用它。

一个几件事情;被这意味着名称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()