[英]how to return a function value that is in a dictionary
我有一個tkinter項目,嘗試使未知的Entry數量並在單擊按鈕后獲取其值! 我嘗試了很多方法,但是聲明后無法返回Entry值! 這是我的方法:
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()
但是,字典確實顯示了值,但僅顯示了小部件聲明時的值! 我想在聲明后得到它的價值! 有任何想法嗎?
在輸入框創建期間,您正在運行e.get
。 您需要與其他事件一起運行e.get()
。 您還應該返回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.