0
点赞
收藏
分享

微信扫一扫

那些年踩过的坑2(python:tkinter;json)

想溜了的蜗牛 2022-01-10 阅读 70

还是大作业

1.循环创建checkbutton,后取值

思路:对所有状态为1的checkbutton对象取其text值

一个很简单的想法(动态创建):

for name in testname:
    test = ttk.Checkbutton(name_frame, text=name, style='t3.TCheckbutton', variable=status)

但是会造成选一个就全部选中的问题(因为variable被统一了 呃

并且,checkbutton没有textvariable这个参数,无法get文本信息

解决方法:建立两个list,分别存放text和variable

    for name in testname:
        status = tk.IntVar()
        test = ttk.Checkbutton(name_frame, text=name, style='t3.TCheckbutton', variable=status)
        #………………
        name_text_list.append(name)
        name_status_list.append(status)

(还是蛮麻烦的,但是没找到更好的改进方法)

2.对话框选择文件夹地址,新建txt文件并写入

fileadd = tk.filedialog.askdirectory()
    if fileadd != "":
        file = open(fileadd + "\\名字.json", encoding="utf-8", mode="w")
        file.write(jsondata)
        file.close()

3.使用python创建json文件

    jsondata = json.dumps({},indent=4,ensure_ascii=False)
    file.write(jsondata)
举报

相关推荐

0 条评论