In this Tkinter tutorial, I am going to create a message box that will get displayed after the user has clicked on the button and the ‘No Show Input’ check box is unchecked. The message box will be put under the showHello function as follows:-
def showHello():
if chVarName.get() == 1:
pass
else:
hello_label.configure(text=helo.get() + " " + slogan.get()) # change the text inside the label widget
msgbox.showinfo("Hello World", "Hello Again World!") # show message to the user
Before you want to use the message box widget you will need to import it first.
from tkinter import messagebox as msgbox
The entire program is as follows:-
import tkinter as tk
from tkinter import ttk, scrolledtext
from tkinter import messagebox as msgbox
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
msgbox.showinfo("Hello World", "Hello Again World!") # show message to the user
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 scrolled text widget
scrollWidth = 30
scrollHeight = 3
scroll = scrolledtext.ScrolledText(win, width=scrollWidth, height=scrollHeight, wrap=tk.WORD)
scroll.grid(column=0, row=2, columnspan=2)
# 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:-
