user2242044
user2242044

Reputation: 9213

checkbox won't appear in Tkinter using grid

My Checkbox C1 does not show up and causing the problem to never finish. What's going on here?

from Tkinter import *

class LabeledEntry(Frame):
    def __init__(self, parent, *args, **kargs):
        text = kargs.pop("text")
        Frame.__init__(self, parent)
        Label(self, text=text, justify=LEFT).grid(sticky=W, column=0, row=0)
        Entry(self, *args, **kargs).grid(sticky=E, column=1, row=0)

class User_Input:
    def __init__(self, parent, fields):

        GUIFrame = Frame(parent)
        GUIFrame.pack(expand=True, anchor=NW)

        field_index = 1
        for field in fields:
            self.field = LabeledEntry(GUIFrame, text=field)
            self.field.grid(column=0, row=field_index, pady=1)
            self.field.grid_columnconfigure(index = 0, minsize = 150)
            field_index += 1
        CheckVar1 = IntVar()
        self.C1 = Checkbutton(parent, text = "checkbox1", variable = CheckVar1, onvalue = 1, offvalue = 0)
        self.C1.grid(column=2, row=2)


fields = ['Entry Box 1:', 'Entry Box 2']


root = Tk()
MainFrame = User_Input(root, fields)
root.mainloop()

Upvotes: 1

Views: 307

Answers (2)

maccartm
maccartm

Reputation: 2115

Try changing the .grid call on C1 to .pack(anchor = W). You have already used pack on the parent frame (with GUIFrame) so Tkinter will happily wait forever trying to decide which layout manager to use.

from Tkinter import *

class LabeledEntry(Frame):
    def __init__(self, parent, *args, **kargs):
        text = kargs.pop("text")
        Frame.__init__(self, parent)
        Label(self, text=text, justify=LEFT).grid(sticky=W, column=0, row=0)
        Entry(self, *args, **kargs).grid(sticky=E, column=1, row=0)

class User_Input:
    def __init__(self, parent, fields):

        GUIFrame = Frame(parent)
        GUIFrame.pack(expand=True, anchor=NW)

        field_index = 1
        for field in fields:
            self.field = LabeledEntry(GUIFrame, text=field)
            self.field.grid(column=0, row=field_index, pady=1)
            self.field.grid_columnconfigure(index = 0, minsize = 150)
            field_index += 1
        CheckVar1 = IntVar()
        self.C1 = Checkbutton(parent, text = "checkbox1", variable = CheckVar1, onvalue = 1, offvalue = 0)
        self.C1.pack(anchor = W)


fields = ['Entry Box 1:', 'Entry Box 2']


root = Tk()
MainFrame = User_Input(root, fields)
root.mainloop()

Reference

Upvotes: 2

mike.k
mike.k

Reputation: 3437

Try to create it using Frame and the root, so replace self.C1 = Checkbutton(parent, ... with self.C1 = Checkbutton(GUIFrame, ...

Upvotes: 0

Related Questions