简体   繁体   中英

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.

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.

A couple of things; window is created inside a function which means that the name window is local to that function. 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. I have called it set_display() instead.

As you will change the image in panel it's better to create it in the global namespace. Also, to be able to change it you must keep a reference, ie give it the name panel and then pack it. Otherwise the name panel will point to the return value from pack() which is = 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.

Then I use bind() to hook the keyboard which is a standard method in tkinter widgets. After that I start mainloop() which waits until a key is pressed and then calls 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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