Python3 Tkinter,如何在def内部获取变量?

huangapple go评论53阅读模式
英文:

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&#39;m trying to get the variable &quot;user_get&quot; 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&#39;t really understand it that well)


</details>


# 答案1
**得分**: 0

这里,我已经让`user_input`变量在全局范围内可用。`entered`函数中的`global`关键字允许该函数修改全局变量。现在,当您在输入框中键入内容并按&lt;kbd&gt;Return&lt;/kbd&gt;键时,`user_input`的值会被更新。

我定义了一个示例函数,每当按下按钮时都会打印这个值。请注意,直到您向`Entry`添加文本并按&lt;kbd&gt;Return&lt;/kbd&gt;键之前,将打印一个空字符串!

同样,任何类似于在`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, &#39;end&#39;)  # clear entry contents


def use_value_from_entry():
    &quot;&quot;&quot;Example function to use the value stored in &#39;user_input&#39;&quot;&quot;&quot;
    print(user_input)
    


window = tk.Tk()

user_input = &#39;&#39;  # 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(&#39;&lt;Return&gt;&#39;, entered)  # bind the event handler

# this button runs an example function to get the current value of &#39;user_input&#39;
button = tk.Button(window, text=&#39;Click Me&#39;, command=use_value_from_entry)
button.pack()

window.mainloop()  # run the app

#Stockmage updated this

huangapple
  • 本文由 发表于 2023年2月18日 00:32:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定