英文:
Python3 Tkinter, how do I get a variable inside of a def?
问题
我试图在 def entered(user_input) 内获取变量 "user_get"
import tkinter as tk
from tkinter import ttk
window = tk.Tk()
# 设置窗口属性(用户名输入)
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()
# 文本框(输入)
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(我不太理解它)
<details>
<summary>英文:</summary>
I'm trying to get the variable "user_get" inside the def entered(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()
I have tried:
-return (I don't really understand it that well)
</details>
# 答案1
**得分**: 0
这里,我已经让`user_input`变量在全局范围内可用。`entered`函数中的`global`关键字允许该函数修改全局变量。现在,当您在输入框中键入内容并按<kbd>Return</kbd>键时,`user_input`的值会被更新。
我定义了一个示例函数,每当按下按钮时都会打印这个值。请注意,直到您向`Entry`添加文本并按<kbd>Return</kbd>键之前,将打印一个空字符串!
同样,任何类似于在`root.mainloop()`之前立即调用`print(user_input)`的代码都会打印一个空字符串,因为`user_input`的值尚未更新。
```python
import tkinter as tk
def entered(_event):
global user_input # 允许该函数修改这个变量
user_input = entry.get() # 用输入框的文本更新变量
entry.delete(0, 'end') # 清除输入框内容
def use_value_from_entry():
"""示例函数,用于使用存储在'user_input'中的值"""
print(user_input)
window = tk.Tk()
user_input = '' # 在全局范围内创建一个变量以存储用户输入
entry = tk.Entry(window) # 创建输入框小部件
entry.pack() # 将小部件添加到界面
entry.bind('<Return>', entered) # 绑定事件处理程序
# 此按钮运行一个示例函数,以获取'user_input'的当前值
button = tk.Button(window, text='点击我', command=use_value_from_entry)
button.pack()
window.mainloop() # 运行应用程序
# Stockmage 更新了这部分
英文:
Here I've made it so the user_input
variable is available in the global scope. The global
keyword in the entered
function allows that function to modify the global variable. Now, when you type into the entry and hit <kbd>Return</kbd>, the value of user_input
is updated.
I defined an example function that will print this value whenever a button is pressed. Note that until you add text to the Entry
and press <kbd>Return</kbd>, an empty string will be printed!
Likewise, any calls like print(user_input)
made immediately before root.mainloop()
will print an empty string because the value of user_input
hasn't been updated yet.
import tkinter as tk
def entered(_event):
global user_input # allow this function to modify this variable
user_input = entry.get() # update the variable with the entry text
entry.delete(0, 'end') # clear entry contents
def use_value_from_entry():
"""Example function to use the value stored in 'user_input'"""
print(user_input)
window = tk.Tk()
user_input = '' # create a variable to store user input in the global scope
entry = tk.Entry(window) # create the entry widget
entry.pack() # add the widget to the UI
entry.bind('<Return>', entered) # bind the event handler
# this button runs an example function to get the current value of 'user_input'
button = tk.Button(window, text='Click Me', command=use_value_from_entry)
button.pack()
window.mainloop() # run the app
#Stockmage updated this
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论