Python Tkinter Hello World Example

Python Tkinter is the python built-in GUI application development library, it is very easy and flexible to use to create simple GUI applications. This article will tell you how to use the Tkinter library to develop a GUI application with a hello world example.

1. How To Use The Python Tkinter To Create GUI Applications.

  1. The Tkinter library is a python built-in library, so you do not need to install it, you can use it directly in your python source code.
  2. What you need to do is just import the tkinter library in your python source code.
    import tkinter as tk
  3. You can import the tkinter library components when you need to use them like below.
    # Import tkinter constants.
    from tkinter.constants import TOP,BOTH,BOTTOM
    
    # Import tkinter's messagebox module.
    from tkinter import messagebox
  4. Below are the python tkinter GUI application creation steps in python source code.
  5. Create the window object using the tkinter library’s function Tk(), and you can call the window object’s method to set the window’s title, size, etc.
    tk_base_window =tk.Tk()
  6. Create a UI component such as a button using the tkinter library’s function Button(), you can call the button object’s various methods to set the button’s text, text style, foreground and background color, etc.
    button=tk.Button(tk_base_window,text = button_text,font = button_text_font,command = tk_base_window.quit)
  7. You can define a python function as the button event handler function.
    def button_click_handler(event):
        # When the button is pressed, it will show a message box.
        messagebox.showinfo('Hello', 'Tkinter hello world.')
  8. And then bind the button event handler function to the button with the button object’s bind() function.
    button.bind('<ButtonPress>', button_click_handler)
  9. Then you can add the button to the window by invoking the button’s pack() function.
    button.pack(side=TOP, fill=BOTH)
  10. Then you can display the window by invoking the tkinter window object’s mainloop() function.
    tk_base_window.mainloop()

2. Python Tkinter Hello World Example.

  1. This example contains a label and a button in the window.
  2. When you click or press the button, it will display a message dialog.
  3. When you close the message dialog, the window will be closed and the program quit.
  4. I use Eclipse + Pydev to develop this example.
  5. The example python module’s name is TkinterHelloWorld. You can read the article How To Run Python In Eclipse With PyDev to learn more.
  6. Then copy the below source code and paste the code to the module file, you can see the code comments for the detailed explanation.
    '''
    Created on Jan 29, 2022
    
    @author: songzhao
    '''
    
    # Import the tkinter library.
    import tkinter as tk
    
    # Import tkinter constants.
    from tkinter.constants import TOP,BOTH,BOTTOM
    
    # Import tkinter's messagebox module.
    from tkinter import messagebox
    
    # Create the main window by invoke the tkinter module's Tk() function.
    tk_base_window =tk.Tk()
    
    # This function is used listen and handle ButtonPress event.
    def button_click_handler(event):
        # When the button is pressed, it will show a message box.
        messagebox.showinfo('Hello', 'Tkinter hello world.')
        
        tk_base_window.quit()
    
    def tk_hello_world():
        
        # Set the main window's size (widthxheight). Concat width and height use 'x' not use "*"
        window_size = '1000x300'
        tk_base_window.geometry(window_size)
    
        # Set the main window's title.
        window_title = 'Tkinter hello world example from dev2qa.com'
        tk_base_window.title(window_title)
    
        # Change the window icon that is displayed on the left corner of the window.
        # logo_image_file_path = '/Users/songzhao/Downloads/favicon.ico'
        logo_image_file_path = 'C:/Users/zhaosong/Downloads/favicon.ico'
        tk_base_window.iconbitmap(logo_image_file_path)
    
        # Set the background color of the main window. The color value can be an English word or a hexadecimal number of the color value.
        # In addition, you can also use the built-in color constant of TK
        window_background_color = 'yellow'
        tk_base_window["background"] = window_background_color
    
        # Add text label.
        label_text = 'Welcome to tkinter.'
        # Set the label background color.
        label_bg_color = "green"
        # Set the label text foreground color
        label_fg_color = "red"
        # Set label text's font, size, and style.
        label_text_font = ('Times', 90, 'bold italic')
        text_label=tk.Label(tk_base_window,text=label_text, bg=label_bg_color, fg=label_fg_color, font=label_text_font)
        # Add the text label to the window top and the label will fill both the window width and height.
        text_label.pack(side=TOP, fill=BOTH)
    
        # Set the button text.
        button_text = "Close"
        # Set the button text's font and size.
        button_text_font = ('Times', 100)
        # Set the button text color.
        button_foreground = 'blue'
        # Set the button background color.
        button_background = 'red'
        # Create the Button object from the Tkinter module with the provided input parameters.
        button=tk.Button(tk_base_window, text = button_text, foreground = button_foreground, background = button_background, font = button_text_font)
        # Bind the ButtonPress event to the function button_click_handler, when the button is pressed, it will invok the function button_click_handler.
        button.bind('', button_click_handler)
        
        # Add the button to the window top below the text label, the button will fill the window width and height.
        button.pack(side=TOP, fill=BOTH)
        
        # Invoke the window's mainloop() function to display the window.
        tk_base_window.mainloop()
    
    
    if __name__ == '__main__':
        
        tk_hello_world()
  7. When you run the above example source code, you can see the tkinter window like below.
    python tkinter hello world example

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.