简体   繁体   English

在python中再次出现多个复选框

[英]multiple checkboxes again in python

I am a noob, I am trying to create many checkboxes at once.我是一个菜鸟,我试图一次创建许多复选框。 ultimately, my program would create folders in the same directory, depending on which ones have been checked.最终,我的程序将在同一目录中创建文件夹,具体取决于已检查的文件夹。 I simply do not get the IntVar() use.我只是没有得到 IntVar() 的使用。 I would love for some of the boxes to be ticked by default...according to the entries in my dictionary I even tried to put the "variable" = 1 in my loop and the boxes are not ticked!!!我希望默认情况下会勾选某些框...根据我字典中的条目,我什至尝试将“变量”= 1 放入我的循环中,但未勾选这些框!!!

import os
from tkinter import *


def create_folders():
    for item in list_of_folders:
        print(item)
            # os.mkdir(list_of_folders[item])


list_of_folders = {"Audio":0,"AVI":0, "Footage":1, "GFX":1, "MP4":1, "MOV":1, "MPG":0,  "Photography":1, "Press":1}
num_row=0
window = Tk()
var=[]
window.title("Folder Creation v1.0")
window.minsize(300, 200)
window.config(padx=100, pady=100)

for item in list_of_folders:
    num_row +=1
    Checkbutton(window, text=item, variable=list_of_folders[item]).grid(row=num_row, sticky=W)



Label(window, text="").grid(column=0, row=10, sticky=W)

Button(window, text="Create Folders", command=create_folders).grid(column=0, row=15, sticky=W)

Button(window, text="Exit", command=window.destroy).grid(column=1, row=15, sticky=W)

window.mainloop()

You was so damn close to the victory there is nothing fancy that I added, I just optimized your code a bit and used the IntVar(value=1) to check the button and stored the variable of that IntVar() into a list to later iterate on final button click to actually create the directories.你太接近胜利了,我没有添加任何花哨的东西,我只是优化了你的代码并使用IntVar(value=1)检查按钮并将该IntVar()的变量存储到列表中以备后用迭代最终按钮单击以实际创建目录。

Calling the get method on the IntVar() returns the current state of the checkbox as you can see in the screenshot.如屏幕截图所示,调用IntVar()上的 get 方法会返回复选框的当前状态。

import os
from tkinter import *


def create_folders():
    for button in boxes:
        if button["state"].get(): #check if its checked or not
            print("Creating folder", button["text"])
            os.mkdir(button["text"])


boxes = []

list_of_folders = {
    "Audio": 0,
    "AVI": 0, 
    "Footage": 1, 
    "GFX": 1, 
    "MP4": 0, 
    "MOV": 1,
    "MPG": 0,  
    "Photography": 1,
    "Press": 1
}

num_row=0
window = Tk()
window.title("Folder Creation v1.0")
window.minsize(300, 200)
window.config(padx=100, pady=100)

for item in list_of_folders.keys():
    lookup = IntVar(value=list_of_folders[item])
    Checkbutton(window, text = item, variable=lookup).grid(row=len(boxes) + 1, sticky=W)
    boxes.append({ 
        "text": item, #store name to create the directory
        "state": lookup #store reference to check whether we need to create directory
    })



Label(window, text="").grid(column=0, row=10, sticky=W)

Button(window, text="Create Folders", command=create_folders).grid(column=0, row=15, sticky=W)

Button(window, text="Exit", command=window.destroy).grid(column=1, row=15, sticky=W)
window.mainloop()

Screenshot截屏

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM