python - 将复选框存储到列表 tkinter
问题描述
我对 tkinter 相当陌生,能够获得以下代码。
from tkinter import *
from tkinter import filedialog
master = Tk()
v = IntVar()
v.set(1)
var1 = IntVar()
var2 = IntVar()
var3 = IntVar()
CategorySubmit= IntVar()
my_list = [{"Kitchen":var1},{"Electric Supply":var2},{"Books":var3}]
def get_boxes(value):
#store the checked boxes for later use
pass
def get_value(value):
#print(value)
if value=="Category":
for widget in master.winfo_children():
widget.destroy()
row =1
for i in my_list:
Checkbutton(master, text=list(i.keys())[0], variable=list(i.values())[0]).pack()
row+=1
Button(master, text="Submit", variable=CategorySubmit,command=get_boxes).pack()
#save the check boxes made by user into a list then quit master
else:
file_chosen =filedialog.askopenfilename(title='Please Select Input File', filetypes=[('Excel files', ('.xlsx', '.csv', '.xls', 'xlsm'))])
print(f"Done: {file_chosen}")
master.destroy()
MODES = [
("Choice 1","ID"),
("Choice 2","Category"),
("Choice 2","Full"),
]
choice_made= StringVar()
choice_made.set('Choice 1')
for text,mode in MODES:
Radiobutton(master,text=text,variable=choice_made,value=mode).pack()
but = Button(master,text="Submit",command=lambda: get_value(choice_made.get()))
but.pack()
master.mainloop()
print(file_chosen) #gives undefined error ?
我需要将复选框中的值存储到列表中,以便以后使用。当对于外部的变量名时,我也有一个错误master.mainloop()
,它给出了NameError: name 'file_chosen' is not defined
,
My idea is when "Choice 1" or "Choice 3" are picked a fileprompt is given so I can continue with my script later on, else if "Choice 2" 3 checkboxes that I store in a list
解决方案
该file_chosen
变量在函数内部是局部的get_value()
。要扩展其范围,您必须声明为全局:
def get_value(value):
global file_chosen
推荐阅读
- javascript - AngularJS:重新加载页面时如何使用会话存储
- asp.net-mvc - 如何使 Windows Server 托管 MVC 应用程序公开
- c# - 检索数据数据网格以形成
- javascript - 在 div 上滚动而不触发整页滚动
- laravel - 在请求验证文件中获取 url 变量
- python - 马尔可夫链蒙特卡罗积分和无限循环
- javascript - React Native DuplicateError:重复的文件或模拟
- mysql - 显示在表格中,只有最后的雕像
- flutter - Flutter:将+图标添加到自动完成?
- java - 如何修复“SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。”