0

我目前正在编写一个程序,当将数字输入数字键盘时,该程序将使用 gui 窗口显示图像。每当我尝试在代码运行时进行选择时,我都会不断收到 Tkinter 错误。我当前的代码和错误在下面。通过 Thonny 使用的 Python 也是 Python 3.9.2。在树莓派 4 上。

#Imports Tkinter (Allows for Python GUIs)
from tkinter import *

#Loads the Python Imaging Library 
from PIL import ImageTk, Image 

##############################################################################

#Displaying Jet Image
def open_jet_image(): 
    # Select the Path from the open_jet_image Function
    global jet_image
    #VECTOR TUBS
    if entry.get() == "00":
        jet_image = "/home/pi/Desktop/Jet Orientation Program/Jet Picture Guides/abc1.png"
    elif entry.get() == "01":
        jet_image = "/home/pi/Desktop/Jet Orientation Program/Jet Picture Guides/abc2.png"
    elif entry.get() == "02":
        jet_image = "/home/pi/Desktop/Jet Orientation Program/Jet Picture Guides/abc3.png"
    elif entry.get() == "03":
        jet_image = "/home/pi/Desktop/Jet Orientation Program/Jet Picture Guides/abc4.png"
    elif entry.get() == "04":
        jet_image = "/home/pi/Desktop/Jet Orientation Program/Jet Picture Guides/abc5.png"
    elif entry.get() == "05":
        jet_image = "/home/pi/Desktop/Jet Orientation Program/Jet Picture Guides/abc6.png"
        
    
    #CLOSE PROGRAM
    elif entry.get() == "99999":
        root.destroy() 
               
    #Imports canvas Variable for use in Function
    global canvas
    
    #Clears the Canvas of Previous Image
    canvas.delete("all")
    
    #Opens the Image
    img = Image.open(jet_image) 
    
    #Resizes the Opened Image
    img = img.resize((1070, 1770), Image.ANTIALIAS) 
    
    #Prepares the Image for the Tkinter GUI Window
    canvas.image = ImageTk.PhotoImage(img)  
    
    #Adds the Image to the Tkinter GUI Window in the Correct Spot
    canvas.create_image(0, 0, image = canvas.image, anchor = "nw")
    canvas.pack()

##############################################################################

#Clears the Inputed Number to Prepare for Next Entry   
def delete():
    entry.delete(0, END)
 
##############################################################################

# Creating the GUI Window
root = Tk() 

# Sets the Title of the Window (Upper Left-Hand Corner of the Window)
root.title("Jet Orientation Program") 

#Setting the Size and Postion of the Window on the Screen
root.geometry("1080x1920")
root.geometry("+0+0")

#Allows the Window size to be Manipulated
root.resizable(width = True, height = True)

#Creates a Blank Canvas for the Image to be Displayed On
canvas = Canvas(bg = "white", height = 1770, width = 1060)
canvas.pack(fill = "both")

##############################################################################

#Displaying Entry Box, asking for Tub Number Input
entry = Entry(root, width=10, font = "Arial 25", justify = "center")

#Sets the Cursor on the Entry Box; aka Ready for Entry, No need for a Mouse
entry.focus_set()
entry.pack()

#Displays a Button to Submit the Users Input from the Entry Box
btn = Button(root, text ='Enter Tub Number', command = lambda:[open_jet_image(), delete()])
btn.pack()

#Binds the Enter Key to Clicking the Button; aka No need for a Mouse
root.bind('<KP_Enter>', lambda event=None: btn.invoke())

##############################################################################

#Runs the program on a loop
root.mainloop() 

###############################################################################

错误如下。

Exception in Tkinter callback
Traceback (most recent call last):
  File "usr/lib/python3.9/tkinter/___init___.py",line 1892,__in__call__
     return self.func(*args)
  File "/home/pi/Desktop/Jet Orientation Program.py",line 135 in <lambda>
    btn = Button(root, text = 'Enter Tub number', command = lamda:[open_jet_image(), delete()])
  File "/home/pi/Desktop/Jet Orientation Program.py",line 88, in open_jet_image
     img = Image.open(jet_image)
 NameError : name 'jet_image' is not defined

4

0 回答 0