jan
jan

Reputation: 143

Checkbutton widget in Tkinter (OOP)

The following is an excerpt of a my first python project I am currently working on.

I am able to add the check button into the GUI, but it is practically useless because I can't find out how I can get the value from the checkbutton.

import tkinter as tk
from tkinter import *
class base(tk.Tk):

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

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

        #tk.Tk.iconbitmap(self, "iconz.ico")
        tk.Tk.wm_title(self, "Mandelbrot Renderer")

        container = tk.Frame(self)
        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, MainPage):
            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()

class MainPage(tk.Frame):
    def getcheckvalue(self):
        print (self.mvar.get())

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Graph Page!", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = ttk.Button(self, text="Back to Home",
                        command=lambda: controller.show_frame(StartPage))
        button1.pack()

        button2 = ttk.Button(self, text="Re-Render",
                        command=self.getcheckvalue)
        button2.pack()
        mvar = IntVar()
        self.cbutton = ttk.Checkbutton(self, text="shadow",onvalue=1, offvalue=0, variable=mvar) 
        self.cbutton.pack()


app = base()
app.geometry ("800x600")
app.mainloop()

Upvotes: 0

Views: 636

Answers (1)

Lafexlos
Lafexlos

Reputation: 7735

mvar = IntVar()

In this line you are creating a local mvar. To access it from outside of your __init__ method, you need to make it class variable by adding self. prefix.

self.mvar = IntVar()
self.cbutton = ttk.Checkbutton(...,  variable=self.mvar) 

Upvotes: 1

Related Questions