[求助]python tkinter checkbutton状态捕捉问题

import tkinter
from tkinter import *


def set_filetype():

    tips = "默认同步常用office文件:\n" \
           "'.txt', '.doc', '.docx', '.dot', '.rtf',\n" \
           " '.xltx', '.xls', '.xlsm', '.xlsb',\n" \
           " '.ppt', '.pptx', '.xps'\n" \
           "如需更改,请选择或添加需要同步的文件后缀名!"

    swindow = tkinter.Tk()
    swindow.title('设置同步文件类型')
    swindow.geometry("500x500+500+150")
    Label(swindow, text=tips, bg="lightyellow", fg="red", justify='left').grid(row=0, columnspan=10)
    tword = ('文档文件类型', ('.txt', '.doc', '.docx', '.dot', '.rtf'))
    texcel = ('表格文件类型', ('.xltx', '.xls', '.xlsm', '.xlsb'))
    tppt = ('演示文稿类型', ('.ppt', '.pptx', '.xps'))
    tpicture = ('图片类型', ('.gif', '.jpg', '.jpeg', '.bmp', '.png', '.raw'))
    tmusic = ('音乐类型', ('.wav', '.mp3', '.flac', '.ape', '.cue'))
    tmovie = ('电影类型', ('.mov', '.mp4', '.rmvb', '.dvd', '.mkv'))
    toptype = {0: tword, 1: texcel, 2: tppt, 3: tpicture, 4: tmusic, 5: tmovie}
    check = {}
    x = {0: toptype[0], 1: toptype[1], 2: toptype[2]}
    flag1 = [0 * len(toptype) for _ in range(len(toptype))]
    typebox = ''
    y = ()
    for i in range(len(toptype)):
        check[i] = IntVar() 

    check[0].set(1)
    check[1].set(1)
    check[2].set(1)
    print(check)

    tc1 = tkinter.Checkbutton(swindow, text=toptype[0][0], variable=check[0], command=lambda: initcheck2(0))
    tc1.grid(row=1, column=0, sticky='w')
    for i in range(len(toptype[0][1])):
        tkinter.Checkbutton(swindow, text=toptype[0][1][i], state=DISABLED,
                            variable=check[0]).grid(row=1, column=i + 1, sticky='w')

    tc2 = tkinter.Checkbutton(swindow, text=toptype[1][0], variable=check[1], command=lambda: initcheck2(1))
    tc2.grid(row=2, column=0, sticky='w')
    for i in range(len(toptype[1][1])):
        tkinter.Checkbutton(swindow, text=toptype[1][1][i], state=DISABLED,
                            variable=check[1]).grid(row=2, column=i + 1, sticky='w')

    tc3 = tkinter.Checkbutton(swindow, text=toptype[2][0], variable=check[2], command=lambda: initcheck2(2))
    tc3.grid(row=3, column=0, sticky='w')
    for i in range(len(toptype[2][1])):
        tkinter.Checkbutton(swindow, text=toptype[2][1][i], state=DISABLED,
                            variable=check[2]).grid(row=3, column=i + 1, sticky='w')

    tc4 = tkinter.Checkbutton(swindow, text=toptype[3][0], variable=check[3], command=lambda: initcheck2(3))
    tc4.grid(row=4, column=0, sticky='w')
    for i in range(len(toptype[3][1])):
        tkinter.Checkbutton(swindow, text=toptype[3][1][i], state=DISABLED,
                            variable=check[3]).grid(row=4, column=i + 1, sticky='w')

    tc5 = tkinter.Checkbutton(swindow, text=toptype[4][0], variable=check[4], command=lambda: initcheck2(4))
    tc5.grid(row=5, column=0, sticky='w')
    for i in range(len(toptype[4][1])):
        tkinter.Checkbutton(swindow, text=toptype[4][1][i], state=DISABLED,
                            variable=check[4]).grid(row=5, column=i + 1, sticky='w')

    tc6 = tkinter.Checkbutton(swindow, text=toptype[5][0], variable=check[5], command=lambda: initcheck2(5))
    tc6.grid(row=6, column=0, sticky='w')
    for i in range(len(toptype[5][1])):
        tkinter.Checkbutton(swindow, text=toptype[5][1][i], state=DISABLED,
                            variable=check[5]).grid(row=6, column=i + 1, sticky='w')

    def initcheck2(m):
        print(check[m].get())
        if check[m].get() == 1:
            flag1[m] = 1
            x[m] = toptype[m]
            print('您选择了!' + str(toptype[m][0]))
        else:
            flag1[m] = 0
            print('您取消了选择!')
            del x[m]
        print(flag1[m])
        typebox = ''
        for k in range(len(toptype)):
            if k in x.keys():
                typebox += x[k][0] + ':\t' + ''.join(z + '\t' for z in x[k][1]) + '\n'
        print(typebox)
        tb.set(typebox)

    def send():
        global filetype
        filetype = y

    tb = tkinter.StringVar()
    Label(swindow, textvariable=tb, bg="lightyellow", fg="red", justify='left').grid(row=len(toptype) + 1, rowspan=6,
                                                                                     columnspan=10)
    button2 = tkinter.Button(swindow, text="确定", width=10, command=send)
    button2.grid(row=len(toptype) + 8)
    swindow.mainloop()


# 调用

set_filetype()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值