python - 如何返回字典中的函数值
问题描述
我有一个 tkinter 项目,它试图在单击按钮后创建未知数量的条目并获取它们的值!我尝试了很多方法,但声明后我无法返回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)
推荐阅读
- django - 带有 WebRTC 的 Django 频道的 STUN/TURN 服务器配置
- node.js - 如何在无限循环中中断 puppeteer-cluster 执行?
- html - 我有两种样式,一种是顺风,另一种是 style.css,但它们表现不佳
- compatibility - 当我通过wireshark 2.2.5和3.4.5从同一个.cap文件生成不同的文本文件时
- firebase - 如何为 GCP 服务帐户提供正确的权限以更新 Firestore 和 Storage 规则?
- flutter - 带有 jsonserializable 包的 iconData
- php - 从 chart.js 1 升级到 3.5 的问题
- java - Jacoco 忽略类引用
- javascript - 带有 Ajax 表单提交按钮的 Foreach 条件
- encryption - 利萨如曲线可以用于数据压缩或加密吗?