Look and Feel Customization on Python Tkinter

by Apr 8, 2020Python Tkinter Examples

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

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()

This image has an empty alt attribute; its file name is image-49.png
This is the python GUI window
This image has an empty alt attribute; its file name is image-50.png
Show info messagebox

Besides showinfo messageboxes, show warning and show error message boxes can also be created.

This is a showerror messagebox
This is a showwarning messagebox

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.

This is an askyesno messagebox

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.

Creating a multiplication Skill in Alexa using python

Written By Monisha Macharla

Hi, I'm Monisha. I am a tech blogger and a hobbyist. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning!

RELATED POSTS

Tkinter based GUI- Application form

Tkinter based GUI- Application form

Hello everyone! Today we are going to make a Tkinter based GUI that will be an Application Form . We will ask some details by the user and then will be saving that with a default extension of .yaml, which can be changed at the time of saving the file. Let's go ahead!...

Layout Management using Python Tkinter

Layout Management using Python Tkinter

In this tutorial, you will be learning “How to layout your GUI using Python”. The following are the topics that will be covered in this article. Python Tkinter Label FramesTkinter Geometry MatrixCreating menu barsCreating tabbed widgets 1. Python Tkinter Label Frames:...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....