Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22873

IslandT: Tkinter Tutorial — Create Check Button Widget

$
0
0

In this Tkinter tutorial let us create a check button widget within the previously created top-level window. These lines of code will create the check box widget.

# Create a check button
chVarName = tk.IntVar()
check0 = tk.Checkbutton(win, text="No Show Input", variable=chVarName)
check0.select()
check0.grid(column=2, row=1)

The above check button will be used to decide whether to replace the text within the label widget or not. As you might have expected the chVarName value will be used to get the state of the check button (checked or un checked), if checked then the program will not show the replacing text on the label widget.

def showHello():
    if chVarName.get() == 1:
        pass
    else:
        hello_label.configure(text=helo.get() + " " + slogan.get()) # change the text inside the label

The entire program is as follows:-

import tkinter as tk
from tkinter import ttk

win =  tk.Tk()
win.title("Hello World!")

def showHello():
    if chVarName.get() == 1:
        pass
    else:
        hello_label.configure(text=helo.get() + " " + slogan.get()) # change the text inside the label

hello = ttk.Button(win, text="Hello!", command=showHello) # create a button
hello.grid(column=0, row=0)

hello_label = ttk.Label(win, text="Hello!") # create a label
hello_label.grid(column=1, row=0)

# create a text box widget
helo = tk.StringVar() # create the helo string variable object
entry_text = ttk.Entry(win, width=16, textvariable=helo)
entry_text.grid(column=0, row=1)

# create a hello world combo box
slogan = tk.StringVar()
helo_combo = ttk.Combobox(win, width=16, textvariable=slogan, state='readonly') # readonly term will avoid the user from typing value into the combo box
helo_combo['value'] = ('Hello', 'Hello Again', 'Hellowin') # create the combo box selection values
helo_combo.grid(column=1, row=1)
helo_combo.current(0)

# Create a check button
chVarName = tk.IntVar()
check0 = tk.Checkbutton(win, text="No Show Input", variable=chVarName)
check0.select()
check0.grid(column=2, row=1)

win.mainloop()

The outcome is as follows:-

If you like my tutorial don’t forget to share it on other social media because I do need lots of readers to motivate myself to continue preparing tutorials which indeed takes lots of my time and energy to do so. Thank you in advance for your help!


Viewing all articles
Browse latest Browse all 22873

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>