简体   繁体   English

Python-如何从 tkinter 小部件获取值并将其分配给变量

[英]Python-How to get the value from tkinter widget and assign it to a variable

from tkinter import *
import tkinter as tk
import pyodbc

root1 = tk.Tk()

label1 = tk.Label(root1, text='product A')
entry1 = tk.Entry(root1)

label1.pack(side = tk.TOP)
entry1.pack()
input1= StringVar()
input1.set(entry1.get())
print (input1)

This code is used to assign the value from the input textbox widget to a variable-Input1.此代码用于将输入文本框小部件的值分配给变量 Input1。 However,the value I get is:PY_VAR0 instead of the text in Entry1.但是,我得到的值是:PY_VAR0 而不是 Entry1 中的文本。

I need to print the input text.我需要打印输入文本。 Why is PY_VAR0 appearing?为什么会出现 PY_VAR0?

Please help.请帮忙。

Thank you.谢谢你。

The problem is that you're getting the value of the entry widget before the user has a chance to type anything, so it will always be the empty string.问题是您在用户有机会输入任何内容之前获取了条目小部件的值,因此它始终是空字符串。

If you wait to do the get until after the user enters something, your code will work fine as-is.如果您等到用户输入内容后才执行 get,您的代码将按原样正常工作。 Though, I don't see any reason to use a StringVar as it just adds an extra object that serves no real purpose.不过,我认为没有任何理由使用StringVar因为它只是添加了一个没有实际用途的额外对象。 There's no reason to use a StringVar with an entry widget unless you need the extra features that a StringVar gets you -- namely, variable traces.没有理由将StringVar与条目小部件一起使用,除非您需要StringVar您提供的额外功能——即变量跟踪。

The reason you are seeing PY_VAR0 is because you must use the get method to get the value out of an instance of StringVar .您看到PY_VAR0的原因是您必须使用get方法从StringVar的实例中获取值。 Change your statement to print input1.get() .将您的语句更改为print input1.get()

To get the contents of a StringVar call get() :要获取StringVar调用get()

input1.get()

Also, you should bind your StringVar to the Entry otherwise the StringVar won't change with the contents of the Entry widget:此外,您应该将StringVar绑定到Entry否则StringVar不会随着Entry小部件的内容而改变:

entry1.config(textvariable=input1) 

Or you can bind at construction:或者您可以在构造时绑定:

input1 = StringVar()
entry1 = tk.Entry(root1, textvariable=input1)

If you want to print the value of a Tkinter variable, you must use the .get() method to retrieve it, as in如果要打印 Tkinter 变量的值,则必须使用.get()方法来检索它,如

print(input1.get())

Otherwise, you are accessing the variable's name, which is what you're seeing.否则,您正在访问变量的名称,这就是您所看到的。

When you have a StringVar (or other similar Tkinter variable), you can get the name of the variable by casting to a string, such as当您有一个 StringVar(或其他类似的 Tkinter 变量)时,您可以通过强制转换为字符串来获取该变量的名称,例如

my_string_variable_name = str(input1)

This is the easiest way to get the variable name if you didn't set it when creating the variable.如果在创建变量时没有设置它,这是获取变量名称的最简单方法。 You can set a variable name as follows:您可以按如下方式设置变量名称:

sv = tkinter.StringVar(name='Fred', value='Flintstone') # Note you can set a value, too!

Then然后

print(str(sv)) # which is what is happening when you say print(sv)

prints out 'Fred', and打印出“弗雷德”,然后

print(sv.get())

prints out 'Flintstone'打印出“燧石”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM