簡體   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