This tutorial will be about Tkinter Checkbuttons. First, make sure that you can run this code, and than we will explain it:
from tkinter import *
import tkinter.messagebox as mb
import os
top_win = Tk()
top_win.geometry("800x800")
gen_lab = Label(top_win, text = "Engine Boost")
gen_lab.grid(row = 0, column = 0)
turbo_10 = IntVar()
turbo_20 = IntVar()
ch_10 = Checkbutton(top_win, text = "Boost +10", variable = turbo_10)
ch_10.grid(row = 1, column = 0)
ch_20 = Checkbutton(top_win, text = "Boost +20", variable = turbo_20)
ch_20.grid(row = 2, column = 0)
but_1 = Button(top_win, text = "Activate Boost")
but_1.grid(row = 3, column = 0)
top_win.mainloop()
Just as with Listbox Widget, we will need Labels for Checkbuttons:
gen_lab = Label(top_win, text = "Engine Boost")
gen_lab.grid(row = 0, column = 0)
And than we will need 2 variables that will be used to hold values:
turbo_10 = IntVar()
turbo_20 = IntVar()
Now we need 2 Checkbuttons. Please note variable = that is connected to corresponding variables. Checkbuttons, as other Tkinter widgets must be positioned. In this case with grid option:
ch_10 = Checkbutton(top_win, text = "Boost +10", variable = turbo_10)
ch_10.grid(row = 1, column = 0)
ch_20 = Checkbutton(top_win, text = "Boost +20", variable = turbo_20)
ch_20.grid(row = 2, column = 0)
And of course, submit button:
but_1 = Button(top_win, text = "Activate Boost")
but_1.grid(row = 3, column = 0)
So far, so good.
Custom functions for Tkinter Checkbuttons
Make sure that you can run this code, and then we will explain it. You will see results in Python Shell:
from tkinter import *
import tkinter.messagebox as mb
import os
top_win = Tk()
top_win.geometry("800x800")
def activate_boost():
status_10 = turbo_10.get()
status_20 = turbo_20.get()
if status_10 == 1:
print("Boost +10 Activated")
if status_20 == 1:
print("Boost +20 Activated")
if status_10 == 1 and status_20 == 1:
print("!" * 50)
print("ALERT - DANGER")
print("Lower Down Boost Levels - Just One Checkbutton")
if status_10 == 0 and status_20 == 0:
print("Normal Operation")
gen_lab = Label(top_win, text = "Engine Boost")
gen_lab.grid(row = 0, column = 0)
turbo_10 = IntVar()
turbo_20 = IntVar()
ch_10 = Checkbutton(top_win, text = "Boost +10", variable = turbo_10)
ch_10.grid(row = 1, column = 0)
ch_20 = Checkbutton(top_win, text = "Boost +20", variable = turbo_20)
ch_20.grid(row = 2, column = 0)
but_1 = Button(top_win, text = "Activate Boost", command = activate_boost)
but_1.grid(row = 3, column = 0)
top_win.mainloop()
This is our custom function that will process what is checked:
def activate_boost():
status_10 = turbo_10.get()
status_20 = turbo_20.get()
if status_10 == 1:
print("Boost +10 Activated")
if status_20 == 1:
print("Boost +20 Activated")
if status_10 == 1 and status_20 == 1:
print("!" * 50)
print("ALERT - DANGER")
print("Lower Down Boost Levels - Just One Checkbutton")
if status_10 == 0 and status_20 == 0:
print("Normal Operation")
With status_10 and status_20 we will get choice. Then we are checking if only one status is set to 1, and printing appropriate text.
If both of statuses are 1, than we print a message: "Lower Down Boost Levels - Just One Checkbutton".
If both statuses are 0, which means they are not operational, than some imaginary machine is working in a normal mode, no any boost, and we are printing "Normal operation".
You are strongly advised to check corresponding YT tutorial on Checkbuttons.
No comments:
Post a Comment