StringVar变量不返回值
在以下代码段中,我希望我的TKINTER程序从文本字段中获取值并将其存储在变量中。但是问题在于,存储的值是默认值stringvar()
变量值(''),而不是文本字段中的值。我尝试使用get()
方法而没有成功。
代码: -
var_stor = [hpw_cb.cget(key='onvalue'), mil_cb.cget(key='onvalue')]
car1_hpw = StringVar()
car1_mil = StringVar()
car1_val = []
# For Car1 Window
hpw_lb_c1 = Label(c1, text="Horsepower")
hpw_ifl_c1 = Entry(c1, textvariable=car1_hpw)
mil_lb_c1 = Label(c1, text="Mileage")
mil_ifl_c1 = Entry(c1, textvariable=car1_mil)
c1_ftr_grp = [hpw_lb_c1, hpw_ifl_c1, mil_lb_c1, mil_ifl_c1]
for i in var_stor:
if i == "Horsepower":
c1_ftr_grp[0].pack(padx=12, pady=20)
c1_ftr_grp[1].pack(padx=12, pady=20)
car1_hpw_str = car1_hpw.get()
car1_val.append(car1_hpw_str)
print(car1_hpw.get())
elif i == "Mileage":
c1_ftr_grp[2].pack(padx=12, pady=20)
c1_ftr_grp[3].pack(padx=12, pady=20)
car1_mil_str = car1_mil.get()
car1_val.append(car1_mil_str)
print(car1_mil.get())
print(car1_val)
问题变量是: - car1_hpw,car1_mil
如何纠正此问题?
In the following code snippet, I want my Tkinter program to get the values from text fields and store it in variables. But the problem is, the values stored are default StringVar()
variable values ('') and not the ones from the text field. I tried using get()
method without success.
The code:-
var_stor = [hpw_cb.cget(key='onvalue'), mil_cb.cget(key='onvalue')]
car1_hpw = StringVar()
car1_mil = StringVar()
car1_val = []
# For Car1 Window
hpw_lb_c1 = Label(c1, text="Horsepower")
hpw_ifl_c1 = Entry(c1, textvariable=car1_hpw)
mil_lb_c1 = Label(c1, text="Mileage")
mil_ifl_c1 = Entry(c1, textvariable=car1_mil)
c1_ftr_grp = [hpw_lb_c1, hpw_ifl_c1, mil_lb_c1, mil_ifl_c1]
for i in var_stor:
if i == "Horsepower":
c1_ftr_grp[0].pack(padx=12, pady=20)
c1_ftr_grp[1].pack(padx=12, pady=20)
car1_hpw_str = car1_hpw.get()
car1_val.append(car1_hpw_str)
print(car1_hpw.get())
elif i == "Mileage":
c1_ftr_grp[2].pack(padx=12, pady=20)
c1_ftr_grp[3].pack(padx=12, pady=20)
car1_mil_str = car1_mil.get()
car1_val.append(car1_mil_str)
print(car1_mil.get())
print(car1_val)
The problem variables are:- car1_hpw, car1_mil
How to rectify this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它无法立即获得价值。您需要为其使用一个按钮。将按钮调用一个函数,然后使用该值,然后使用它做事。我还看到您正在使用条目。条目不需要
StringVar()
来获取值。它只需要存储在变量中的对象的名称。It cannot get the value instantly. You need to use a button for it. Make button call a function where you get the value and then you do stuff with it. I also see you are using entries. Entry does not need a
StringVar()
to get the value. it just needs the name of an object that you stored into a variable.您可以从许多不同的事件触发功能。这取决于需要什么。下面是3种可能的方法,但仅是一个样本。
You can trigger the function from many different events. It depends what is required. Below the are 3 possible ways but its only a sample.