如果我在一个函数中创建了小部件,我如何使用 Python Tkinter 在另一个函数中访问它们?
If I created widgets in one function, how can I access them in another function using Python Tkinter?
这是我第一个使用 Tkinter 的项目,所以如果问题很容易解决,请原谅。
根据用户从下拉列表中选择的选项,我调用了一个函数来创建并将某些小部件(例如条目)放置在框架上。然后,当按下另一个按钮时,我想访问该条目中的文本。但是,这似乎给我错误(说小部件未定义),因为我想访问调用函数时创建的小部件。
我看到的一个明显的解决方案是创建我想在函数外部使用的所有可能的小部件,并且只在函数被调用时放置它们。这看起来很草率并且会产生更多问题。还有其他修复方法吗?
提前致谢!
这是我创建小部件并将其放置在框架上的函数。
def loadBook():
print("book is loaded")
#Authors
labelAuth1 = tk.Label(frame, text="Author 1 Name:")
entryAuth1 = tk.Entry(frame)
labelAuth1.place(relwidth=0.23, relheight=0.08, rely=0.1)
entryAuth1.place(relheight=0.08, relwidth=0.18, relx=0.3, rely=0.1)
这是一个函数片段,它使用我在上面创建的条目小部件的输入:
def isBook():
if len(entryAuthSur1.get())==0:
pass
else:
bookString = ""
bookString += entryAuthSur1.get()
当第二个函数执行时,我收到一个运行时错误,指出 entryAuthSur1
未定义。
函数内的所有变量都是局部变量。这意味着它在函数调用结束后被删除。由于您的变量 (entryAuth1
) 不是全局变量,因此它仅存在于函数内部,并在 loadBook
函数结束时被删除。这是工作代码:
import tkinter as tk
# Making a window for the widgets
root = tk.Tk()
def loadBook():
global entryAuth1 # make the entry global so all functions can access it
print("book is loaded")
#Authors
labelAuth1 = tk.Label(root, text="Author 1 Name:")
entryAuth1 = tk.Entry(root)
# I will check if the user presses the enter key to run the second function
entryAuth1.bind("<Return>", lambda e: isBook())
labelAuth1.pack()
entryAuth1.pack()
def isBook():
global entryAuth1 # make the entry global so all functions can access it
# Here you had `entryAuthSur1` but I guess it is the same as `entryAuth1`
if len(entryAuth1.get())==0:
pass
else:
bookString = ""
bookString += entryAuth1.get()
print(bookString) # I am going to print the result to the screen
# Call the first function
loadBook()
root.mainloop()
这是我第一个使用 Tkinter 的项目,所以如果问题很容易解决,请原谅。 根据用户从下拉列表中选择的选项,我调用了一个函数来创建并将某些小部件(例如条目)放置在框架上。然后,当按下另一个按钮时,我想访问该条目中的文本。但是,这似乎给我错误(说小部件未定义),因为我想访问调用函数时创建的小部件。
我看到的一个明显的解决方案是创建我想在函数外部使用的所有可能的小部件,并且只在函数被调用时放置它们。这看起来很草率并且会产生更多问题。还有其他修复方法吗?
提前致谢!
这是我创建小部件并将其放置在框架上的函数。
def loadBook():
print("book is loaded")
#Authors
labelAuth1 = tk.Label(frame, text="Author 1 Name:")
entryAuth1 = tk.Entry(frame)
labelAuth1.place(relwidth=0.23, relheight=0.08, rely=0.1)
entryAuth1.place(relheight=0.08, relwidth=0.18, relx=0.3, rely=0.1)
这是一个函数片段,它使用我在上面创建的条目小部件的输入:
def isBook():
if len(entryAuthSur1.get())==0:
pass
else:
bookString = ""
bookString += entryAuthSur1.get()
当第二个函数执行时,我收到一个运行时错误,指出 entryAuthSur1
未定义。
函数内的所有变量都是局部变量。这意味着它在函数调用结束后被删除。由于您的变量 (entryAuth1
) 不是全局变量,因此它仅存在于函数内部,并在 loadBook
函数结束时被删除。这是工作代码:
import tkinter as tk
# Making a window for the widgets
root = tk.Tk()
def loadBook():
global entryAuth1 # make the entry global so all functions can access it
print("book is loaded")
#Authors
labelAuth1 = tk.Label(root, text="Author 1 Name:")
entryAuth1 = tk.Entry(root)
# I will check if the user presses the enter key to run the second function
entryAuth1.bind("<Return>", lambda e: isBook())
labelAuth1.pack()
entryAuth1.pack()
def isBook():
global entryAuth1 # make the entry global so all functions can access it
# Here you had `entryAuthSur1` but I guess it is the same as `entryAuth1`
if len(entryAuth1.get())==0:
pass
else:
bookString = ""
bookString += entryAuth1.get()
print(bookString) # I am going to print the result to the screen
# Call the first function
loadBook()
root.mainloop()