我试图在def中获取变量"user_get"(user_input)
import tkinter as tk
from tkinter import ttk
window = tk.Tk()
#put settings in place (username input)
window.geometry('1920x1080')
window.configure(bg = 'blue')
def entered(user_input):
user_get = user_input.widget.get()
user_input.widget.delete(0, 'end')
print(user_get)
return user_get
user_input.widget.destroy()
# TextBox (input)
user_input = tk.Entry(window)
user_input.pack()
user_input.place(x = 100,y = 40)
user_input.bind("<Return>", entered)
thing = user_get
print(thing)
window.mainloop()
我试过:
- return(我不是很理解)
1条答案
按热度按时间aiazj4mn1#
在这里,我设置了
user_input
变量在全局作用域中可用,entered
函数中的global
关键字允许该函数修改全局变量,现在,当您键入条目并点击Return时,user_input
的值将被更新。我定义了一个示例函数,每当按下一个按钮时,它都会打印这个值。注意,在您向
Entry
添加文本并按下Return之前,将打印一个空字符串!同样,在
root.mainloop()
之前执行的任何类似print(user_input)
的调用都将打印空字符串,因为user_input
的值还没有更新。