DEV Community

importostar
importostar

Posted on

tkinter gui

You learn the basic idea of the Python GUI programming via the tkinter module in this post. This tutorial is mainly designed to understand the simple tkinter concept that allows you to build any Python GUI.

GUI: GUI is a basic program that allows users to communicate with their machine through a graphical interface.

GUI tkinter module

tkinter is a built-in Python framework for GUI development.

Python offers a variety of GUI choices like tkinter, pyqt and others.

The most popular way to create a graphical application is tkinter.

Apps are constructed like this:

  • Import the tkinter module
  • Build the window of the GUI application
  • In the main window of a Interface program, add some amount of widgets
  • Start the main loop
import tkinter as tk
from tkinter import *

Example = tk.Tk()

rad1=Radiobutton(Example, text='Content', value=1)
rad1.pack()
rad2=Radiobutton(Example, text='Software', value=2)
rad2.pack()
label=Button(Example,text='Welcome to Example', width=100)
label.pack()


Example.mainloop()

tk example

.mainloop() when ready to run a program. This shows the window until it's closed.

Widgets can be placed in 3 ways:

  1. pack():
    the widgets put in the widget parent.

  2. grid():
    the grid widgets are put in the parent widget.

  3. place():
    positions them at different points that we control.

Tkinter lets you place all kinds of widgets like buttons, radiobuttons, listbox etc.

# button
button_name=tkinter,Button(
       parent,  
       text='your text', 
       width='width_of_text', 
       command='function to call')

# radio button    
rad1=Radiobutton(
       parent, 
       text='your text', 
       value=numerical_value).pack()

# listbox
List=Listbox(parent)
List.insert(1, text)
List.insert(2, nexttext)

# Entry
label1=Label(parent, text='Name').grid(row=0)
label2=Label(parent, text='password').grid(row=1)
entry_in_label1 = Entry(parent)
entry_in_label2 = Entry(parent)
entry_in_label1.grid(row=0, column=1)
entry_in_label2.grid(row=1, column=1)

You can make a GUI for any type of code, like a bmi calculator, notifications, progressbar, canvas etc.

If you are new to tkinter, I recommend this course

Top comments (1)

Collapse
 
shivamkumraa9 profile image
shivamkumraa9

Thank you for these code examples. BTW I have also created a login form GUI by using Tkinter. Here is the picture of that. For more such valuable code examples. Please kindly refer to my blog rawpython.com