两个复选框单击时检查

时间:2018-04-19 20:17:43

标签: python tkinter

我正在为一个名为组织者的项目制作应用程序。这是一个组织应用程序。我有一个问题,当你勾选一个复选框时,他们都会检查。

我该如何解决这个问题?

跑完之后去:清单 - >输入A值 - >点击“添加分配” - >重复几次 - >尝试点击一个

另外,我的.update()有效,但似乎仍然会导致错误?你知道为什么吗?

谢谢!

import tkinter as tk
root = tk


AsgnList = []
#Initialization
class TheOrganizer(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)


        container = tk.Frame(self)         
        self.geometry('500x500')        
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (StartPage, AddAsgnPage):        

            frame = F(container, self)    
            self.frames[F] = frame    
            frame.grid(row=0, column=0, sticky="nsew")


        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]               
        frame.tkraise()


def RunApp():
    app = TheOrganizer()    
    app.title('The Organizer')        
    app.mainloop()





#Making New Pages
'''
Make sure for very new page, you add it to the 'for loop'
'''


HeadFont = ("Verdana", 40)

class StartPage(tk.Frame):    

    def __init__(self, parent, controller):

        tk.Frame.__init__(self,parent)

        LabTitle = tk.Label(self, text="The Organizer", font=HeadFont)      
        LabTitle.pack()

        AddAsgnBtn = tk.Button(self, text='Checklist', command=lambda: controller.show_frame(AddAsgnPage))  
        AddAsgnBtn.place(x=100,y=250)

class AddAsgnPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        LabTitle = tk.Label(self, text="Assignments", font=HeadFont)      
        LabTitle.pack()

        #Input Assignment        
        def getEntry():
            entryInput = AsgnEntry.get()
            AsgnList.append(entryInput)
            yVal = 300
            while 1:
                var = tk.IntVar()        
                for z in AsgnList:             
                    tk.Checkbutton(self, text=z, variable=var).place(x=200,y=yVal)                   
                    yVal += 25                      
                TheOrganizer.update()


        AsgnEntry = tk.Entry(self)
        AsgnEntry.place(x=175,y=205)

        SubBtn = tk.Button(self, text='Add Assignment', command=getEntry)
        SubBtn.place(x=25,y=200)

        BackBtn = tk.Button(self, text='Back', command=lambda: controller.show_frame(StartPage))
        BackBtn.place(x=250,y=400)

1 个答案:

答案 0 :(得分:1)

您对两个复选框使用相同的变量。将变量创建移动到循环内部,以使用不同的变量。

var_list = []
for z in AsgnList:
    var = tk.IntVar()
    tk.Checkbutton(self, text=z, variable=var).place(x=200,y=yVal)
    yVal += 25         
    var_list.append(var)

也就是说,您可能希望存储变量,以便检查以后标记的复选框。所以我添加了一个var_list列表对象来存储所有创建的var