简体   繁体   English

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

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

My idea in this code is running an app with Tkinter that 'lights on" a Seven Segment Display depending on which number I press on my keyboard.我在这段代码中的想法是使用 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()

I have not worked with the keyboard module, but I can show you how to work without it.我没有使用过keyboard模块,但我可以向您展示如何在没有它的情况下工作。

A couple of things;几件事; window is created inside a function which means that the name window is local to that function. window 是在函数内部创建的,这意味着名称window是该函数的本地名称。 Instead create the window in the global scope.而是在全局范围内创建窗口。 Also the function set() is a builtin function and if you redefine it you will not be able to access the builtin function.此外,函数set()是一个内置函数,如果您重新定义它,您将无法访问内置函数。 I have called it set_display() instead.我把它叫做set_display()

As you will change the image in panel it's better to create it in the global namespace.由于您将在panel更改图像,因此最好在全局命名空间中创建它。 Also, to be able to change it you must keep a reference, ie give it the name panel and then pack it.此外,为了能够更改它,您必须保留一个参考,即给它命名panel ,然后打包。 Otherwise the name panel will point to the return value from pack() which is = None .否则,名称panel将指向pack()的返回值,即 = None

When you later change the image in the label in the function set_display() you must also save a reference to the image in the label, explicitly commented in my example code.当您稍后在函数set_display()更改标签中的图像时,您还必须在标签中保存对图像的引用,在我的示例代码中明确注释。

Then I use bind() to hook the keyboard which is a standard method in tkinter widgets.然后我使用bind()来挂钩键盘,这是 tkinter 小部件中的标准方法。 After that I start mainloop() which waits until a key is pressed and then calls keypress() .之后我启动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