[英]how to return a function value that is in a dictionary
I have a tkinter project that try to make unknown number of Entry and get their value after a button clicked! 我有一个tkinter项目,尝试使未知的Entry数量并在单击按钮后获取其值! I tried many ways, but I can not return the Entry value after declared! 我尝试了很多方法,但是声明后无法返回Entry值! Here is my approach: 这是我的方法:
from tkinter import Entry, Tk, Button
l = [50]
def entry(x, y):
global data
e = Entry()
e.place(x=x, y=y, height=20, width=100)
data = e.get()
return data
def loop():
n = 0
s = l[0]
for_x = 10
for_y = 10
global en
en = dict()
while True:
if n == s:
break
else:
en[n] = entry(for_x, for_y)
n = n + 1
if for_y >= 400:
for_x = for_x + 110
for_y = 10
print("110")
else:
for_y = for_y + 30
print("30")
print("finally")
root = Tk()
root.minsize(500, 500)
loop()
def dp():
print(en)
b = Button(command=dp)
b.place(x=480, y=400)
root.mainloop()
The dictionary however do show the values, but only values that was at time of widget declaration! 但是,字典确实显示了值,但仅显示了小部件声明时的值! I want to get the value of it after declaration! 我想在声明后得到它的价值! Any ideas? 有任何想法吗?
You are running e.get
during the entry box creation. 在输入框创建期间,您正在运行e.get
。 You need to run e.get()
in connection with some other event. 您需要与其他事件一起运行e.get()
。 You also should return the Entry object instead of returning the data for example: 您还应该返回Entry对象,而不是返回数据,例如:
def entry(x, y):
e = Entry()
e.place(x=x, y=y, height=20, width=100)
return e
def loop():
n = 0
s = l[0]
for_x = 10
for_y = 10
global entry_list
entry_list = [] # Used to store all of the entry widgets you make
while True:
if n == s:
break
else:
entry_list.append(entry(for_x, for_y)) # Adds a new entry widget to the list
n = n + 1
if for_y >= 400:
for_x = for_x + 110
for_y = 10
print("110")
else:
for_y = for_y + 30
print("30")
print("finally")
def dp():
# Get the value of each entry box
en = []
for e in entry_list:
en.append(e.get())
print(en)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.