我想知道是否可以从 tkinter 条目中获取值作为列表(包括类型)。
from tkinter import *
window = Tk()
base = '1,None,1'
items = StringVar(window, value=base)
def submit():
result = list(items.get().split(','))
print(result)
Entry(window, bg='white', textvariable=items).place(relx=0.1, rely=0.1, anchor='nw')
Button(window, text='Submit', command=submit).place(relx=0.9, rely=0.9, anchor='se')
window.mainloop()
这将提供输出,['1', 'None', '1']
而我需要的是[1, None, 1]
. 如果解决方案需要遍历列表并检查每个元素如何将字符串更改'None'
为NoneType
?
tkinter 没有内置任何东西可以以字符串形式以外的任何形式返回条目小部件的内容。但是,您当然可以获取该值并根据您想要的任何规则对其进行解析(例如,以空格或逗号分隔)。
在您的特定示例中,由于您明确地为包含 word 的字符串设置了一个值None
,因此您可以检查该特定字符串并进行转换。例如:
def submit():
result = [None if x.strip() == 'None' else x for x in items.get().split(',')]
print(result)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句