Python 2.7 Tkinter/python:如何创建checkbutton以选择所有checkbutton

Python 2.7 Tkinter/python:如何创建checkbutton以选择所有checkbutton,python-2.7,tkinter,Python 2.7,Tkinter,我用Tkinter创建了一个复选框列表,但我想用一个复选框选择所有复选框 以下是我的部分代码: root = tk.Tk() root.title("SOMETHING") buttons=[] #If it is checked, then run the file def callback(): for var, name in buttons: if var.get(): subprocess.call("python " + "scrip

我用Tkinter创建了一个复选框列表,但我想用一个复选框选择所有复选框

以下是我的部分代码:

root = tk.Tk()
root.title("SOMETHING")

buttons=[]
#If it is checked, then run the file
def callback():
    for var, name in buttons:
        if var.get():
            subprocess.call("python " + "scripts/" + name)

for name in os.listdir("scripts"):
    if name.endswith('.py') or name.endswith('.pyc'):
        if name not in ("____.py", "_____.pyc"):
            var = tk.BooleanVar()
            cb = tk.Checkbutton(root, text=name, variable=var)
            cb.pack()
            buttons.append((var,name))

def select_all():
    if var1.get():
        for i in cb:
            i.select(0,END)

def deselect_all():
    if var2.get():
        for i in cb:
            i.deselect_set(0,END)

var1=tk.BooleanVar()
selectButton = tk.Checkbutton(root, text="Select All", command=select_all, variable=var1)
selectButton.pack()

var2=tk.BooleanVar()
deselectButton = tk.Checkbutton(root, text="None", command=deselect_all, variable=var2)
deselectButton.pack()

submitButton = tk.Button(root, text="Run", command=callback)
submitButton.pack()

root.mainloop()
当我运行该文件并按下“全选”时,我得到了这个错误:“str”对象没有“select”属性


请帮助,谢谢:)

我已经查看了您的代码,并附上了一个有效的tkinter代码。将让您使用子流程来解决问题

我的主要意见是,您使用控制变量以及如何使用它来获取和设置它们的值是不合适的。我已经删除了不必要的代码。此外,还向您展示了如何从列表中提取信息。希望这有助于您的tkinter编码之旅

工作代码:

import tkinter as tk
import os, subprocess

#If it is checked, then run the file
def callback():
    if var.get():
        for i in buttons:
            cmd = "python3 " + filedir +"/" + i[1] #Note:Runs python3 and not python2
            print(cmd)
            subprocess.call(cmd)

def select_all(): # Corrected
    for item in buttons:
        v , n = item
        if v.get():
            v.set(0)
        else:
            v.set(1)

root = tk.Tk()
root.title("SOMETHING")

filedir = 'test'
buttons=[]

for name in os.listdir(filedir):
    if name.endswith('.py') or name.endswith('.pyc'):
        if name not in ("____.py", "_____.pyc"):
            var = tk.IntVar() 
            var.set(0)
            cb = tk.Checkbutton(root, text=name, variable=var)
            cb.pack()
            buttons.append((var,name))

var1=tk.IntVar()
var1.set(0)
selectButton = tk.Checkbutton(root, text="Select All", command=select_all,
                              variable=var1)
selectButton.pack()

submitButton = tk.Button(root, text="Run", command=callback)
submitButton.pack()

root.mainloop()

您希望cb:中的i做什么
cb
不是一个列表,它只是您创建的最后一个复选按钮。另外,
.select(0,END)
对Checkbutton不是一个有意义的操作,它看起来像是对条目执行的操作。此外,我认为BooleanVar不适用于您的复选按钮——它们的默认开/关状态是1/0,而不是真/假;试试IntVar。对于这些变量,选择/取消选择按钮的方法是在变量上调用
.set(1)
.set(0)
。@jasonharper def select_all()表示如果选中了“全选”复选框,则检查列表并全选。但我不确定该为该函数编写什么。是的,我把BooleanVar改成了IntVar。谢谢,但当我点击不同的复选框时,它会点击所有内容。@RABBIT My bad。。已更正并上载工作代码。对此有几点意见。“全选”实际上是一个选中的切换。回调代码中有一个bug。if应该在循环中并使用i[0].get(),否则它只是查看最后一个复选框的值。我会修改代码