Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我不知道为什么这个简单的python代码没有运行_Python_Tkinter - Fatal编程技术网

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

我不知道为什么这个简单的python代码没有运行,python,tkinter,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:\User

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

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

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

因为您将在面板中更改图像,所以最好在全局名称空间中创建它。此外,为了能够更改它,您必须保留一个引用,即给它命名面板,然后打包。否则,名称面板将指向pack的返回值,该值为=None

稍后在函数集_display中更改标签中的图像时,还必须在标签中保存对图像的引用,在我的示例代码中有明确的注释

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


那有什么问题?给出一个例子。你将陷入递归侦听器/设置循环,永远无法到达tk.mainloop。问题是什么都没有发生,我真的不知道为什么,我试图不到达tk.mainloop,因为它会停止所有程序,但如果我不把它放进去,代码不起作用类似于此代码为什么不起作用这样的问题对于stackoverflow来说是离题的。你需要更具体一点,你是个天才!这非常好用,非常感谢!!!我将为即将到来的项目修正我的错误,谢谢
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()