Andres Cala
Andres Cala

Reputation: 75

CheckButton on tkinter is not "On" after setting the value

I recently changed the geometry manager in my program from grid to pack because of a problem trying to get the Navigatiotoolbar in the frame.

The problem is that a check button that used to be on by default is not On by default now. Here is the code for the button:

    Var1=IntVar()
    Var1.set(1)
    c = tk.Checkbutton(self,text="Test", variable=Var1, onvalue=1,  offvalue=0)
    c.pack( side = LEFT )

So if I change the geometry manager I cannot longer put an ON default state?

Pd. The program runs without errors.

Here is a more complete piece of the code

class StartPage(tk.Tk):


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

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

    Frame1 = Frame(self)
    Frame1.pack(side=TOP)

    Frame2 = Frame(self)
    Frame2.pack( side= TOP )

    Frame3 = Frame(self)
    Frame3.pack( side = TOP)

    Frame4 = Frame(self)
    Frame4.pack( side = BOTTOM , pady=20, expand=True)

    label = tk.Label(Frame1, text="Start Page")
    label.pack(side=TOP)

    button1=ttk.Button(Frame2, text="Visit Page 1",
                      command=lambda:controller.show_frame(PageOne))
    button1.pack( side = LEFT )


    button2=ttk.Button(Frame3, text="Page Two",
                      command=lambda:controller.show_frame(PageTwo) )
    button2.pack( side = LEFT )


    button3=ttk.Button(Frame2, text="Spider",
                      command=lambda:controller.show_frame(PageThree) )
    button3.pack( side = LEFT )

    button4=ttk.Button(Frame2, text="Ternario",
                      command=lambda:controller.show_frame(PageFour) )
    button4.pack( side = LEFT )

    button5=ttk.Button(Frame3, text="Ti-Zr-Yx3",
                      command=lambda:controller.show_frame(PageFive) )
    button5.pack( side = LEFT )

    #global Var1

    Var1=IntVar()
    Var1.set(1)
    c = tk.Checkbutton(self,text="Test", variable=Var1, onvalue=1, offvalue=0)
    c.pack( side = LEFT )


app = StartPage()
app.mainloop()

Upvotes: 1

Views: 1872

Answers (2)

Questaware
Questaware

Reputation: 79

The variable must be safe from garbage collection but also I think that you need the following code

import tkinter as tk
from tkinter import Tk

def NewCheckbutton(frame, text, myvar, state, row, col):
   setit=myvar.get()
   c = tk.Checkbutton(frame, text=text, variable=myvar,
                      state=state)
   c.grid(row=row, column=col, sticky="EW")
   c.select()
   c.toggle()
   print(f'Set={setit}')
   if setit:
     c.toggle()
   return c

root = Tk()

var1 = tk.IntVar()
#   self.var1 = var1
var1.set(0)
c1 = NewCheckbutton(root, text="One", myvar=var1, 
                    state="normal", row=0, col=0)
var2 = tk.IntVar()
#   self.var2 = var2
var2.set(1)
c2 = NewCheckbutton(root, text="Two", myvar=var2, 
                    state="normal", row=1, col=0)
#   ...
root.mainloop()

Upvotes: 0

Kevin
Kevin

Reputation: 76184

It looks like Var1 is being garbage collected at the end of the __init__ function, making the Checkbutton forget its state before the window begins to render. One possible workaround is to save Var1 as an attribute of the instance, so it doesn't die prematurely.

    self.Var1=IntVar()
    self.Var1.set(1)
    c = tk.Checkbutton(self,text="Test", variable=self.Var1, onvalue=1, offvalue=0)
    c.pack( side = LEFT )

... Or just don't have an IntVar at all.

    c = tk.Checkbutton(self,text="Test")
    c.pack( side = LEFT )
    c.select()

Upvotes: 3

Related Questions