In this tutorial, you will learn about how to customize the look and feel of a python GUI created using tkinter. This is third part of tkinter tutorial course. To begin with, we will look at how to create message boxes.
Contents:
- Creating Messageboxes
- Create independent messageboxes
- Assign title to python GUI window
- Change icon for python GUI window
- Spinbox widget
Creating Messageboxes
A messagebox is a pop-up window that gives feedback to the user. There are various types of messageboxes – informational, hinting at potential problems and ones that take user response. The first type of messagebox we will look at is show info messagebox.
Showinfo messagebox
from tkinter import * #importing tkinter
from tkinter import messagebox as msg #importing messagebox
root = Tk()
#Defining a callback function
def popup():
info = msg.showinfo("This is my popup!", "Hello world")
#first argument is the title, second is the text
#Button that calls the popup function
my_button = Button(root, text = "Pop up", command = popup).pack()
root.mainloop()
Besides showinfo messageboxes, show warning and show error message boxes can also be created.
Now we will see a different type of messagebox, ones that take user input, and store it.
Askyesno messagebox
from tkinter import * #importing tkinter
from tkinter import messagebox as msg #importing messagebox
root = Tk()
def popup(): #Defining a callback function
#first argument is the title, second is the text
response = msg.askyesno("Pop up", "Would you like to see some cautionary rules before using your program?")
if (response == True):
rules = Label(root, text = "You should always double check your code")
rules.pack()
#Button that calls the popup function
my_button = Button(root, text = "Pop up", command = popup).pack()
root.mainloop()
The option selected by the user has a value and you can use that value in your program.
How to create independent messageboxes
This is the recipe to create independent messageboxes without any root window.
from tkinter import *
from tkinter import messagebox as msg
root = Tk() #Creating the root window
root.withdraw() #Getting rid of the root window
messagebox = msg.showinfo("Title", "This is an independent messagebox")
root.mainloop()
root.withdraw() function withdraws the root window and we get an independent message box.
How to assign a title to the python GUI window
This is a very important topic if you are trying to customize your python GUI window. Now we will see how to change the title of our python GUI window.
from tkinter import *
root = Tk()
root.title("Title for my program")
root.geometry("400x200")
root.mainloop()
root.title(“ ”) assigns a title for the window and root.geometry(“”) sets a predefined size for the window.
How to change the icon for python GUI window
To further customize the GUI window, we will see how to change the icon for our python GUI window .
from tkinter import *
root = Tk()
root.title("Title for my program")
root.geometry("400x200")
root.iconbitmap("icon.ico")
root.mainloop()
root.iconbitmap(“file_name”) changes the icon of your window with the one you provided. The one thing to remember is that the icon should be a .ico file.
Spin box widget
The following program shows how to create a spin box widget and elaborates a bit on its functionality.
from tkinter import *
root = Tk()
root.title("Title for my program")
root.iconbitmap("icon.ico")
#defining a variable to set value of entry box
v = StringVar()
#Defining a callback function for set_from_spin
def set_from_spin():
v.set(spin.get())
#defining submit function
def submit():
show_Label = Label(root, text = "You selected " + enter.get())
show_Label.grid(row = 3, column = 1, pady = 8)
info_label = Label(root, text = "You can enter a value between 0 and 10 or you can select one using the spin box and click on the button to enter")
info_label.grid(row = 0, column = 0, columnspan = 3, pady = 8, padx = 5)
#Spinbox
spin = Spinbox(root, from_= 0, to = 10, width = 10, bd = 6) #bd is borderwidth
#width is the spinbox width
spin.grid(row = 1, column = 0)
#Entry label
enter = Entry(root, textvariable = v)
enter.grid(row = 1, column = 1)
btn_set = Button(root, text = "Set value", command = set_from_spin)
btn_set.grid(row = 1, column = 2)
btn_submit = Button(root, text = "Submit", command = submit, width = 10).grid(row = 2, column = 1, pady = 10)
root.mainloop()
The output window for this code is shown below
This program has two labels, two buttons, one entry box and one spin box. You can enter a value into the entry box or choose one from the spin box and set its value. Clicking the submit button the second label. The options padx and pady set values for x and y padding, respectively. We can also create a spin box with specific values.
There are of course, a lot more customizations available. You can discover them as you explore.