Tkinter checkbuttons (part 1)

Let’s start using checkbuttons. Here I put some code to make an image to show wich checbuttons have been selected:

import tkinter as tk
root = tk.Tk()
on_image = tk.PhotoImage()
off_image = tk.PhotoImage()

def printme():
    print("hello")


var1 = tk.IntVar(value=1)
var2 = tk.IntVar(value=0)


def check(intvar):
    return tk.Checkbutton(
        root,
        compound="left",
        text="Hello",
        image=off_image,
        selectimage=on_image,
        indicatoron=True,
        onvalue=1,
        offvalue=0,
        variable=intvar,
        font="arial 20",
        command=printme)


def colors_check():
    root.update()
    root.update()
    c1h = c1.winfo_height()
    c2h = c2.winfo_height()
    on_image.put(("green",), to=(0, 0, 15, c1h))
    off_image.put(("white",), to=(0, 0, 20, c2h))


c1 = check(var1)
c2 = check(var2)
c1.grid(row=0, column=0)
c2.grid(row=1, column=0)
colors_check()


root.mainloop()


Subscribe to the newsletter for updates
Tkinter templates
My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts