栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

各位大神,我用tkinter自己编了个同步文件的小程序自用。基本功能已实现。在添加设置文件类型菜单功能时,遇见一个问题:在主程序里调用该函数,checkbutton状态(即check)怎么都不变;如果单独用该函数运行,则一切正常。实在不理解问题出在哪。请大神指教,万分感谢!该函数代码如下:

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])
        flashx()

    print(x)

    def flashx():
        typebox = ''
        for k in range(len(toptype)):
            print(x)
            if toptype[k] in x:
                typebox += x[k][0] + ''.join(z for z in x[k][1]) + 'n'

    #  flashx()
    print(typebox)

    def send():
        global filetype
        filetype = y

    tb = tkinter.StringVar(swindow, value=typebox)
    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()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/839863.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号