Python GUI Tutorial - Python GUI with Examples - Tkinter Tutorial

Python GUI Tutorial - Python GUI with Examples - Tkinter Tutorial

What is gui & gui based application , what is tkinter, how to install tkinter, how to use different tkinter methods with python to create gui based real world application , api calling and database operations using tkinter & python,how to develop the weather app ,different layout design techniques,api calling ,uses of open weather api , complete end to end project with complete python code.

What is gui & gui based application , what is tkinter, how to install tkinter, how to use different tkinter methods with python to create gui based real world application , api calling and database operations using tkinter & python,how to develop the weather app ,different layout design techniques,api calling ,uses of open weather api , complete end to end project with complete python code.

What you'll learn:

  • Learn how to build gui based application using python tkinter
  • Learn Tkinter module from basic to advance
  • Learn how to use Tkinter to build real world application
  • End to end live project using tkinter and open weather api
  • Build weather application using tkinter and python
  • How to setup database connectivity using tkinter and python
  • How to call rest api using using tkinter and python

Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Python Training

This video on Tkinter tutorial covers all the basic aspects of creating and making use of your own simple Graphical User Interface (GUI) using Python. It establishes all of the concepts needed to get started with building your own user interfaces while coding in Python.

This video on Tkinter tutorial covers all the basic aspects of creating and making use of your own simple Graphical User Interface (GUI) using Python. It establishes all of the concepts needed to get started with building your own user interfaces while coding in Python.

Learn More

☞ Complete Python Bootcamp: Go from zero to hero in Python 3

☞ Complete Python Masterclass

☞ Learn Python by Building a Blockchain & Cryptocurrency

☞ Python and Django Full Stack Web Developer Bootcamp

☞ The Python Bible™ | Everything You Need to Program in Python

☞ Learning Python for Data Analysis and Visualization

☞ Python for Financial Analysis and Algorithmic Trading

☞ The Modern Python 3 Bootcamp

Original video source: https://www.youtube.com/watch?v=VMP1oQOxfM0

Python Tutorial - Python GUI Programming - Python GUI Examples (Tkinter Tutorial)

Python Tutorial - Python GUI Programming - Python GUI Examples (Tkinter Tutorial)

In this tutorial, we will learn how to develop graphical user interfaces by writing some Python GUI examples using the Tkinter package.

In this tutorial, we will learn how to develop graphical user interfaces by writing some Python GUI examples using the Tkinter package.

Tkinter package is shipped with Python as a standard package, so we don’t need to install anything to use it.

Tkinter is a very powerful package. If you already have installed Python, you may use IDLE which is the integrated IDE that is shipped with Python, this IDE is written using Tkinter. Sounds Cool!!

We will use Python 3.6, so if you are using Python 2.x, it’s strongly recommended to switch to Python 3.x unless you know the language changes so you can adjust the code to run without errors.

I assume that you have a little background in the Python basics to help you understand what we are doing.

We will start by creating a window to which we will learn how to add widgets such as buttons, combo boxes, etc. Then we will play with their properties, so let’s get started.

Create Your First GUI Application

First, we will import THE Tkinter package and create a window and set its title:

from tkinter import *

window = Tk()

window.title("Welcome to LikeGeeks app")

window.mainloop()

The result will look like this:

Awesome! Our application works.

The last line calls the mainloop function. This function calls the endless loop of the window, so the window will wait for any user interaction till we close it.

If you forget to call the mainloop function, nothing will appear to the user.

Create a Label Widget

To add a label to our previous example, we will create a label using the label class like this:

lbl = Label(window, text="Hello")

Then we will set its position on the form using the grid function and give it the location like this:

lbl.grid(column=0, row=0)

So the complete code will be like this:

from tkinter import *

window = Tk()

window.title("Welcome to LikeGeeks app")

lbl = Label(window, text="Hello")

lbl.grid(column=0, row=0)

window.mainloop()

And this is the result:

Without calling the grid function for the label, it won’t show up.

Set Label Font Size

You can set the label font so you can make it bigger and maybe bold. You can also change the font style.

To do so, you can pass the font parameter like this:

lbl = Label(window, text="Hello", font=("Arial Bold", 50))

Note that the font parameter can be passed to any widget to change its font, thus it applies to more than just labels.

Great, but the window is so small, what about setting the window size?

Setting Window Size

We can set the default window size using the geometry function like this:

window.geometry('350x200')

The above line sets the window width to 350 pixels and the height to 200 pixels.

Let’s try adding more GUI widgets like buttons and see how to handle button click events.

Adding a Button Widget

Let’s start by adding the button to the window. The button is created and added to the window in the same way as the label:

btn = Button(window, text="Click Me")

btn.grid(column=1, row=0)

So our window will be like this:

from tkinter import *

window = Tk()

window.title("Welcome to LikeGeeks app")

window.geometry('350x200')

lbl = Label(window, text="Hello")

lbl.grid(column=0, row=0)

btn = Button(window, text="Click Me")

btn.grid(column=1, row=0)

window.mainloop()

The result looks like this:

Note that we place the button on the second column of the window, which is 1. If you forget and place the button on the same column which is 0, it will show the button only, since the button will be on the top of the label.

Change Button Foreground and Background Colors

You can change the foreground of a button or any other widget using the fg property.

Also, you can change the background color of any widget using the bg property.

btn = Button(window, text="Click Me", bg="orange", fg="red")

Now, if you tried to click on the button, nothing happens because the click event of the button isn’t written yet.

Handle Button Click Event

First, we will write the function that we need to execute when the button is clicked:

def clicked():

    lbl.configure(text="Button was clicked !!")

Then we will wire it with the button by specifying the function like this:

btn = Button(window, text= "Click Me", command=clicked)

Note that, we typed clicked only not clicked() with parentheses.

Now the full code will be like this:

from tkinter import *

window = Tk()

window.title("Welcome to LikeGeeks app")

window.geometry('350x200')

lbl = Label(window, text="Hello")

lbl.grid(column=0, row=0)

def clicked():

    lbl.configure(text="Button was clicked !!")

btn = Button(window, text="Click Me", command=clicked)

btn.grid(column=1, row=0)

window.mainloop()

And when we click the button, the result, as expected, looks like this:

Cool!

Get Input Using Entry Class (Tkinter Textbox)

In the previous Python GUI examples, we saw how to add simple widgets, now let’s try getting the user input using the Tkinter Entry class (Tkinter textbox).

You can create a textbox using Tkinter Entry class like this:

txt = Entry(window,width=10)

Then you can add it to the window using a grid function as usual

So our window will be like this:

from tkinter import *

window = Tk()

window.title("Welcome to LikeGeeks app")

window.geometry('350x200')

lbl = Label(window, text="Hello")

lbl.grid(column=0, row=0)

txt = Entry(window,width=10)

txt.grid(column=1, row=0)

def clicked():

    lbl.configure(text="Button was clicked !!")

btn = Button(window, text="Click Me", command=clicked)

btn.grid(column=2, row=0)

window.mainloop()

And the result will be like this:

Now, if you click the button, it will show the same old message, but what about showing the entered text on the Entry widget?

First, you can get entry text using the get function. So we can write this code to our clicked function like this:

def clicked():

    res = "Welcome to " + txt.get()

    lbl.configure(text= res)

If you click the button and there is text in the entry widget, it will show “Welcome to” concatenated with the entered text.

And this is the complete code:

from tkinter import *

window = Tk()

window.title("Welcome to LikeGeeks app")

window.geometry('350x200')

lbl = Label(window, text="Hello")

lbl.grid(column=0, row=0)

txt = Entry(window,width=10)

txt.grid(column=1, row=0)

def clicked():

    res = "Welcome to " + txt.get()

    lbl.configure(text= res)

btn = Button(window, text="Click Me", command=clicked)

btn.grid(column=2, row=0)

window.mainloop()

Run the above code and check the result:

Awesome!

Every time we run the code, we need to click on the entry widget to set focus to write the text, but what about setting the focus automatically?

Set the Focus of the Entry Widget

That’s super easy, all we need to do is to call the focus function like this:

txt.focus()

And when you run your code, you will notice that the entry widget has the focus so you can write your text right away.

Disable the Entry Widget

To disable the entry widget, you can set the state property to disabled:

txt = Entry(window,width=10, state='disabled')

Now, you won’t be able to enter any text.

Add a Combobox Widget

To add a combobox widget, you can use the Combobox class from ttk library like this:

from tkinter.ttk import *

combo = Combobox(window)

Then you can add your values to the combobox.

from tkinter import *

from tkinter.ttk import *

window = Tk()

window.title("Welcome to LikeGeeks app")

window.geometry('350x200')

combo = Combobox(window)

combo['values']= (1, 2, 3, 4, 5, "Text")

combo.current(1) #set the selected item

combo.grid(column=0, row=0)

window.mainloop()

As you can see, we add the combobox items using the values tuple.

To set the selected item, you can pass the index of the desired item to the current function.

To get the select item, you can use the get function like this:

combo.get()

Add a Checkbutton Widget (Tkinter Checkbox)

To create a checkbutton widget, you can use the Checkbutton class like this:

chk = Checkbutton(window, text='Choose')

Also, you can set the checked state by passing the check value to the Checkbutton like this:

from tkinter import *

from tkinter.ttk import *

window = Tk()

window.title("Welcome to LikeGeeks app")

window.geometry('350x200')

chk_state = BooleanVar()

chk_state.set(True) #set check state

chk = Checkbutton(window, text='Choose', var=chk_state)

chk.grid(column=0, row=0)

window.mainloop()

Check the result:

Set the Check State of a Checkbutton

Here we create a variable of type BooleanVar which is not a standard Python variable, it’s a Tkinter variable, and then we pass it to the Checkbutton class to set the check state as the highlighted line in the above example.

You can set the Boolean value to false to make it unchecked.

Also, you can use IntVar instead of BooleanVar and set the value to 0 or 1.

chk_state = IntVar()

chk_state.set(0) #uncheck

chk_state.set(1) #check

These examples give the same result as the BooleanVar.

Add Radio Button Widgets

To add radio buttons, you can use the RadioButton class like this:

rad1 = Radiobutton(window,text='First', value=1)

Note that you should set the value for every radio button with a different value, otherwise, they won’t work.

from tkinter import *

from tkinter.ttk import *

window = Tk()

window.title("Welcome to LikeGeeks app")

window.geometry('350x200')

rad1 = Radiobutton(window,text='First', value=1)

rad2 = Radiobutton(window,text='Second', value=2)

rad3 = Radiobutton(window,text='Third', value=3)

rad1.grid(column=0, row=0)

rad2.grid(column=1, row=0)

rad3.grid(column=2, row=0)

window.mainloop()

The result of the above code looks like this:

Also, you can set the command of any of these radio buttons to a specific function, so if the user clicks on any one of them, it runs the function code.

This is an example:

rad1 = Radiobutton(window,text='First', value=1, command=clicked)

def clicked():

# Do what you need

Pretty simple!

Get Radio Button Values (Selected Radio Button)

To get the currently selected radio button or the radio button’s value, you can pass the variable parameter to the radio buttons and later you can get its value.

from tkinter import *

from tkinter.ttk import *

window = Tk()

window.title("Welcome to LikeGeeks app")

selected = IntVar()

rad1 = Radiobutton(window,text='First', value=1, variable=selected)

rad2 = Radiobutton(window,text='Second', value=2, variable=selected)

rad3 = Radiobutton(window,text='Third', value=3, variable=selected)

def clicked():

   print(selected.get())

btn = Button(window, text="Click Me", command=clicked)

rad1.grid(column=0, row=0)

rad2.grid(column=1, row=0)

rad3.grid(column=2, row=0)

btn.grid(column=3, row=0)

window.mainloop()

Every time you select a radio button, the value of the variable will be changed to the value of the selected radio button.

Add a ScrolledText Widget (Tkinter textarea)

To add a ScrolledText widget, you can use the ScrolledText class like this:

from tkinter import scrolledtext

txt = scrolledtext.ScrolledText(window,width=40,height=10)

Here we specify the width and the height of the ScrolledText widget, otherwise, it will fill the entire window.

from tkinter import *

from tkinter import scrolledtext

window = Tk()

window.title("Welcome to LikeGeeks app")

window.geometry('350x200')

txt = scrolledtext.ScrolledText(window,width=40,height=10)

txt.grid(column=0,row=0)

window.mainloop()

The result as you can see:

Set Scrolledtext Content

To set scrolledtext content, you can use the insert method like this:

txt.insert(INSERT,'You text goes here')

Delete/Clear Scrolledtext Content

To clear the contents of a scrolledtext widget, you can use the delete method like this:

txt.delete(1.0,END)

Great!

Create a Message Box

To show a message box using Tkinter, you can use the messagebox library like this:

from tkinter import messagebox

messagebox.showinfo('Message title','Message content')

Pretty easy!

Let’s show a message box when the user clicks a button.

from tkinter import *

from tkinter import messagebox

window = Tk()

window.title("Welcome to LikeGeeks app")

window.geometry('350x200')

def clicked():

    messagebox.showinfo('Message title', 'Message content')

btn = Button(window,text='Click here', command=clicked)

btn.grid(column=0,row=0)

window.mainloop()

When you click the button, an informative message box will appear.

Show Warning and Error Messages

You can show a warning message or error message the same way. The only thing that needs to be changed is the message function

messagebox.showwarning('Message title', 'Message content')  #shows warning message

messagebox.showerror('Message title', 'Message content')    #shows error message

Show Ask Question Dialogs

To show a yes/no message box to the user, you can use one of the following messagebox functions:

from tkinter import messagebox

res = messagebox.askquestion('Message title','Message content')

res = messagebox.askyesno('Message title','Message content')

res = messagebox.askyesnocancel('Message title','Message content')

res = messagebox.askokcancel('Message title','Message content')

res = messagebox.askretrycancel('Message title','Message content')

You can choose the appropriate message style according to your needs. Just replace the showinfo function line from the previous line and run it.

Also, you can check what button was clicked using the result variable.

If you click OK or yes or retry, it will return True as the value, but if you choose no or cancel, it will return False.

The only function that returns one of three values is the askyesnocancel function; it returns True or False or None.

Add a SpinBox (Numbers Widget)

To create a Spinbox widget, you can use the Spinbox class like this:

spin = Spinbox(window, from_=0, to=100)

Here we create a Spinbox widget and we pass the from_ and to parameters to specify the numbers range for the Spinbox.

Also, you can specify the width of the widget using the width parameter:

spin = Spinbox(window, from_=0, to=100, width=5)

Check the complete example:

from tkinter import *

window = Tk()

window.title("Welcome to LikeGeeks app")

window.geometry('350x200')

spin = Spinbox(window, from_=0, to=100, width=5)

spin.grid(column=0,row=0)

window.mainloop()

You can specify the numbers for the Spinbox instead of using the whole range like this:

spin = Spinbox(window, values=(3, 8, 11), width=5)

Here the Spinbox widget only shows these 3 numbers: 3, 8, and 11.

Set a Default Value for Spinbox

To set the Spinbox default value, you can pass the value to the textvariable parameter like this:

var =IntVar()

var.set(36)

spin = Spinbox(window, from_=0, to=100, width=5, textvariable=var)

Now, if you run the program, it will show 36 as a default value for the Spinbox.

Add a Progressbar Widget

To create a progress bar, you can use the progressbar class like this:

from tkinter.ttk import Progressbar

bar = Progressbar(window, length=200)

You can set the progress bar value like this:

bar['value'] = 70

You can set this value based on any process you want like downloading a file or completing a task.

Change Progressbar Color

Changing the Progressbar color is a bit tricky.

First, we will create a style and set the background color and finally set the created style to the Progressbar.

Check the following example:

from tkinter import *

from tkinter.ttk import Progressbar

from tkinter import ttk

window = Tk()

window.title("Welcome to LikeGeeks app")

window.geometry('350x200')

style = ttk.Style()

style.theme_use('default')

style.configure("black.Horizontal.TProgressbar", background='black')

bar = Progressbar(window, length=200, style='black.Horizontal.TProgressbar')

bar['value'] = 70

bar.grid(column=0, row=0)

window.mainloop()

And the result will look like this:

Add a File Dialog (File and Directory Chooser)

To create a file dialog (file chooser), you can use the filedialog class like this:

from tkinter import filedialog

file = filedialog.askopenfilename()

After you choose a file and click open, the file variable will hold that file path.

Also, you can ask for multiple files like this:

files = filedialog.askopenfilenames()

Specify File Types (Filter File Extensions)

You can specify the file types for a file dialog using the filetypes parameter, just specify the extensions in tuples.

file = filedialog.askopenfilename(filetypes = (("Text files","*.txt"),("all files","*.*")))

You can ask for a directory using the askdirectory method:

dir = filedialog.askdirectory()

You can specify the initial directory for the file dialog by specifying the initialdir like this:

from os import path

file = filedialog.askopenfilename(initialdir= path.dirname(__file__))

Easy!

Add a Menu Bar

To add a menu bar, you can use the menu class like this:

from tkinter import Menu

menu = Menu(window)

menu.add_command(label='File')

window.config(menu=menu)

First, we create a menu, then we add our first label, and, finally, we assign the menu to our window.

You can add menu items under any menu by using the add_cascade() function like this:

menu.add_cascade(label='File', menu=new_item)

So our code will be like this:

from tkinter import *

from tkinter import Menu

window = Tk()

window.title("Welcome to LikeGeeks app")

menu = Menu(window)

new_item = Menu(menu)

new_item.add_command(label='New')

menu.add_cascade(label='File', menu=new_item)

window.config(menu=menu)

window.mainloop()

This way, you can add as many menu items as you want.

from tkinter import *

from tkinter import Menu

window = Tk()

window.title("Welcome to LikeGeeks app")

menu = Menu(window)

new_item = Menu(menu)

new_item.add_command(label='New')

new_item.add_separator()

new_item.add_command(label='Edit')

menu.add_cascade(label='File', menu=new_item)

window.config(menu=menu)

window.mainloop()

Here we add another menu item called Edit with a menu separator.

You may notice a dashed line at the beginning, well, if you click that line, it will show the menu items in a small separate window.

You can disable this feature by disabling the tearoff feature like this:

new_item = Menu(menu, tearoff=0)

Just replace the new_item in the above example with this one and it won’t show the dashed line anymore.

I don’t need to remind you that you can type any code that works when the user clicks on any menu item by specifying the command property.

new_item.add_command(label='New', command=clicked)

Add a Notebook Widget (Tab Control)

To create a tab control, there are a few steps.

  • First, we create a tab control using the Notebook class.
  • Create a tab using the Frame class.
  • Add that tab to the tab control.
  • Pack the tab control so it becomes visible in the window.
from tkinter import *

from tkinter import ttk

window = Tk()

window.title("Welcome to LikeGeeks app")

tab_control = ttk.Notebook(window)

tab1 = ttk.Frame(tab_control)

tab_control.add(tab1, text='First')

tab_control.pack(expand=1, fill='both')

window.mainloop()

In this way, you can add as many tabs as you want.

Add Widgets to Notebooks

After creating tabs, you can put widgets inside these tabs by assigning the parent property to the desired tab.

from tkinter import *

from tkinter import ttk

window = Tk()

window.title("Welcome to LikeGeeks app")

tab_control = ttk.Notebook(window)

tab1 = ttk.Frame(tab_control)

tab2 = ttk.Frame(tab_control)

tab_control.add(tab1, text='First')

tab_control.add(tab2, text='Second')

lbl1 = Label(tab1, text= 'label1')

lbl1.grid(column=0, row=0)

lbl2 = Label(tab2, text= 'label2')

lbl2.grid(column=0, row=0)

tab_control.pack(expand=1, fill='both')

window.mainloop()

Add Spacing for Widgets (Padding)

You can add padding for your controls to make it look well organized using the padx and pady properties.

Just pass padx and pady to any widget and give them a value.

lbl1 = Label(tab1, text= 'label1', padx=5, pady=5)

It’s that simple!

In this tutorial, we saw many Python GUI examples using the Tkinter library and we saw how easy it’s to develop graphical interfaces using it.

This tutorial covered the main aspects of Python GUI development, but not all of them. There is no tutorial or a book that can cover everything.

I hope you found these examples useful. Keep coming back.

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading about Python

Complete Python Bootcamp: Go from zero to hero in Python 3

Machine Learning A-Z™: Hands-On Python & R In Data Science

Python and Django Full Stack Web Developer Bootcamp

Complete Python Masterclass

Python Tutorial - Python GUI Programming - Python GUI Examples (Tkinter Tutorial)

Python Programming Tutorial | Full Python Course for Beginners 2019 👍

Computer Vision Using OpenCV

OpenCV Python Tutorial - Computer Vision With OpenCV In Python

Python Tutorial: Image processing with Python (Using OpenCV)

A guide to Face Detection in Python

*Originally published at *https://likegeeks.com/

Python GUI Tutorial - Python GUI Programming Using Tkinter Tutorial

Python GUI Tutorial - Python GUI Programming Using Tkinter Tutorial

In this article, we will learn how to develop graphical user interfaces (GUI) by writing some Python GUI examples using the Tkinter package.

In this article, we will learn how to develop graphical user interfaces (GUI) by writing some Python GUI examples using the Tkinter package.

Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest way to create the GUI applications. Creating a GUI using tkinter is an easy task.

To create a tkinter:

  1. Importing the module – tkinter
  2. Create the main window (container)
  3. Add any number of widgets to the main window
  4. Apply the event Trigger on the widgets.

Importing tkinter is same as importing any other module in the python code. Note that the name of the module in Python 2.x is ‘Tkinter’ and in Python 3.x is ‘tkinter’.

import tkinter

There are two main methods used you the user need to remember while creating the Python application with GUI.

1 - Tk(screenName=None, baseName=None, className=’Tk’, useTk=1): To create a main window, tkinter offers a method ‘Tk(screenName=None, baseName=None, className=’Tk’, useTk=1)’. To change the name of the window, you can change the className to the desired one. The basic code used to create the main window of the application is:

m=tkinter.Tk() where m is the name of the main window object

2 - mainloop(): There is a method known by the name mainloop() is used when you are ready for the application to run. mainloop() is an infinite loop used to run the application, wait for an event to occur and process the event till the window is not closed.

m.mainloop()

import tkinter 
m = tkinter.Tk() 
''' 
widgets are added here 
'''
m.mainloop() 

tkinter also offers access to the geometric configuration of the widgets which can organize the widgets in the parent windows. There are mainly three geometry manager classes class.

  1. **pack() method:**It organizes the widgets in blocks before placing in the parent widget.
  2. **grid() method:**It organizes the widgets in grid (table-like structure) before placing in the parent widget.
  3. **place() method:**It organizes the widgets by placing them on specific positions directed by the programmer.

There are a number of widgets which you can put in your tkinter application. Some of the major widgets are explained below:

1 - Button:To add a button in your application, this widget is used.

The general syntax is:

w=Button(master, option=value)

master is the parameter used to represent the parent window.

There are number of options which are used to change the format of the Buttons. Number of options can be passed as parameters separated by commas. Some of them are listed below.
activebackground: to set the background color when button is under the cursor.activeforeground: to set the foreground color when button is under the cursor.bg: to set he normal background color.command: to call a function.font: to set the font on the button label.image: to set the image on the button.width: to set the width of the button.height: to set the height of the button.

import tkinter as tk 
r = tk.Tk() 
r.title('Counting Seconds') 
button = tk.Button(r, text='Stop', width=25, command=r.destroy) 
button.pack() 
r.mainloop() 

Output:

**2 - Canvas: **It is used to draw pictures and other complex layout like graphics, text and widgets.

The general syntax is:

w = Canvas(master, option=value)
master is the parameter used to represent the parent window.

There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

  • bd: to set the border width in pixels.
  • bg: to set the normal background color.
  • cursor: to set the cursor used in the canvas.
  • highlightcolor: to set the color shown in the focus highlight.
  • width: to set the width of the widget.
  • height: to set the height of the widget.
from tkinter import *
master = Tk() 
w = Canvas(master, width=40, height=60) 
w.pack() 
canvas_height=20
canvas_width=200
y = int(canvas_height / 2) 
w.create_line(0, y, canvas_width, y ) 
mainloop() 


Output:

**3 - CheckButton: **To select any number of options by displaying a number of options to a user as toggle buttons. The general syntax is:

w = CheckButton(master, option=value)

There are number of options which are used to change the format of this widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

  • Title: To set the title of the widget.
  • activebackground: to set the background color when widget is under the cursor.
  • activeforeground: to set the foreground color when widget is under the cursor.
  • bg: to set he normal backgrouSteganography
  • Break
  • Secret Code:
  • Attach a File:nd color.
  • command: to call a function.
  • font: to set the font on the button label.
  • image: to set the image on the widget.
from tkinter import *
master = Tk() 
var1 = IntVar() 
Checkbutton(master, text='male', variable=var1).grid(row=0, sticky=W) 
var2 = IntVar() 
Checkbutton(master, text='female', variable=var2).grid(row=1, sticky=W) 
mainloop() 


Output:

**4 - Entry:**It is used to input the single line text entry from the user.. For multi-line text input, Text widget is used.

The general syntax is:

w=Entry(master, option=value)

master is the parameter used to represent the parent window.

There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

  • bd: to set the border width in pixels.
  • bg: to set the normal background color.
  • cursor: to set the cursor used.
  • command: to call a function.
  • highlightcolor: to set the color shown in the focus highlight.
  • width: to set the width of the button.
  • height: to set the height of the button.
from tkinter import *
master = Tk() 
Label(master, text='First Name').grid(row=0) 
Label(master, text='Last Name').grid(row=1) 
e1 = Entry(master) 
e2 = Entry(master) 
e1.grid(row=0, column=1) 
e2.grid(row=1, column=1) 
mainloop() 


Output:

5 - Frame: It acts as a container to hold the widgets. It is used for grouping and organizing the widgets. The general syntax is:

w = Frame(master, option=value)
master is the parameter used to represent the parent window.

There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

  • highlightcolor: To set the color of the focus highlight when widget has to be focused.
  • bd: to set the border width in pixels.
  • bg: to set the normal background color.
  • cursor: to set the cursor used.
  • width: to set the width of the widget.
  • height: to set the height of the widget.
from tkinter import *


root = Tk() 
frame = Frame(root) 
frame.pack() 
bottomframe = Frame(root) 
bottomframe.pack( side = BOTTOM ) 
redbutton = Button(frame, text = 'Red', fg ='red') 
redbutton.pack( side = LEFT) 
greenbutton = Button(frame, text = 'Brown', fg='brown') 
greenbutton.pack( side = LEFT ) 
bluebutton = Button(frame, text ='Blue', fg ='blue') 
bluebutton.pack( side = LEFT ) 
blackbutton = Button(bottomframe, text ='Black', fg ='black') 
blackbutton.pack( side = BOTTOM) 
root.mainloop() 


Output:

6 - Label: It refers to the display box where you can put any text or image which can be updated any time as per the code.

The general syntax is:

w=Label(master, option=value)
master is the parameter used to represent the parent window.

There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

  • bg: to set he normal background color.
  • bg to set he normal background color.
  • command: to call a function.
  • font: to set the font on the button label.
  • image: to set the image on the button.
  • width: to set the width of the button.
  • height” to set the height of the button.
from tkinter import *
root = Tk() 
w = Label(root, text='GeeksForGeeks.org!') 
w.pack() 
root.mainloop() 


Output:

7 - Listbox: It offers a list to the user from which the user can accept any number of options.

The general syntax is:

w = Listbox(master, option=value)
master is the parameter used to represent the parent window.

There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

  • highlightcolor: To set the color of the focus highlight when widget has to be focused.
  • bg: to set he normal background color.
  • bd: to set the border width in pixels.
  • font: to set the font on the button label.
  • image: to set the image on the widget.
  • width: to set the width of the widget.
  • height: to set the height of the widget.
from tkinter import *


top = Tk() 
Lb = Listbox(top) 
Lb.insert(1, 'Python') 
Lb.insert(2, 'Java') 
Lb.insert(3, 'C++') 
Lb.insert(4, 'Any other') 
Lb.pack() 
top.mainloop() 


Output:

8 - MenuButton: It is a part of top-down menu which stays on the window all the time. Every menubutton has its own functionality. The general syntax is:

w = MenuButton(master, option=value)
master is the parameter used to represent the parent window.

There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

  • activebackground: To set the background when mouse is over the widget.
  • activeforeground: To set the foreground when mouse is over the widget.
  • bg: to set he normal background color.
  • bd: to set the size of border around the indicator.
  • cursor: To appear the cursor when the mouse over the menubutton.
  • image: to set the image on the widget.
  • width: to set the width of the widget.
  • height: to set the height of the widget.
  • highlightcolor: To set the color of the focus highlight when widget has to be focused.
from tkinter import *


top = Tk() 
mb = Menubutton ( top, text = "GfG") 
mb.grid() 
mb.menu = Menu ( mb, tearoff = 0 ) 
mb["menu"] = mb.menu 
cVar = IntVar() 
aVar = IntVar() 
mb.menu.add_checkbutton ( label ='Contact', variable = cVar ) 
mb.menu.add_checkbutton ( label = 'About', variable = aVar ) 
mb.pack() 
top.mainloop() 


Output:

9 - Menu: It is used to create all kinds of menus used by the application.

The general syntax is:

w = Menu(master, option=value)
master is the parameter used to represent the parent window.

There are number of options which are used to change the format of this widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

  • title: To set the title of the widget.
  • activebackground: to set the background color when widget is under the cursor.
  • activeforeground: to set the foreground color when widget is under the cursor.
  • bg: to set he normal background color.
  • command: to call a function.
  • font: to set the font on the button label.
  • image: to set the image on the widget.
from tkinter import *
	
root = Tk() 
menu = Menu(root) 
root.config(menu=menu) 
filemenu = Menu(menu) 
menu.add_cascade(label='File', menu=filemenu) 
filemenu.add_command(label='New') 
filemenu.add_command(label='Open...') 
filemenu.add_separator() 
filemenu.add_command(label='Exit', command=root.quit) 
helpmenu = Menu(menu) 
menu.add_cascade(label='Help', menu=helpmenu) 
helpmenu.add_command(label='About') 
mainloop() 


Output:

10 - Message: It refers to the multi-line and non-editable text. It works same as that of Label.

The general syntax is:

w = Message(master, option=value)
master is the parameter used to represent the parent window.

There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

  • bd: to set the border around the indicator.
  • bg: to set he normal background color.
  • font: to set the font on the button label.
  • image: to set the image on the widget.
  • width: to set the width of the widget.
  • height: to set the height of the widget.
from tkinter import *
main = Tk() 
ourMessage ='This is our Message'
messageVar = Message(main, text = ourMessage) 
messageVar.config(bg='lightgreen') 
messageVar.pack( ) 
main.mainloop( ) 


Output:

**11 - RadioButton: **It is used to offer multi-choice option to the user. It offers several options to the user and the user has to choose one option.

The general syntax is:

w = RadioButton(master, option=value)

There are number of options which are used to change the format of this widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

  • activebackground: to set the background color when widget is under the cursor.
  • activeforeground: to set the foreground color when widget is under the cursor.
  • bg: to set he normal background color.
  • command: to call a function.
  • font: to set the font on the button label.
  • image: to set the image on the widget.
  • width: to set the width of the label in characters.
  • height: to set the height of the label in characters.
from tkinter import *
root = Tk() 
v = IntVar() 
Radiobutton(root, text='GfG', variable=v, value=1).pack(anchor=W) 
Radiobutton(root, text='MIT', variable=v, value=2).pack(anchor=W) 
mainloop() 


Output:

**12 - Scale: **It is used to provide a graphical slider that allows to select any value from that scale. The general syntax is:

w = Scale(master, option=value)
master is the parameter used to represent the parent window.

There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

  • cursor: To change the cursor pattern when the mouse is over the widget.
  • activebackground: To set the background of the widget when mouse is over the widget.
  • bg: to set he normal background color.
  • orient: Set it to HORIZONTAL or VERTICAL according to the requirement.
  • from_: To set the value of one end of the scale range.
  • to: To set the value of the other end of the scale range.
  • image: to set the image on the widget.
  • width: to set the width of the widget.
from tkinter import *
master = Tk() 
w = Scale(master, from_=0, to=42) 
w.pack() 
w = Scale(master, from_=0, to=200, orient=HORIZONTAL) 
w.pack() 
mainloop() 


Output:

13 - Scrollbar: It refers to the slide controller which will be used to implement listed widgets.

The general syntax is:

w = Scrollbar(master, option=value)
master is the parameter used to represent the parent window.

There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

  • width: to set the width of the widget.
  • activebackground: To set the background when mouse is over the widget.
  • bg: to set he normal background color.
  • bd: to set the size of border around the indicator.
  • cursor: To appear the cursor when the mouse over the menubutton.
from tkinter import *
root = Tk() 
scrollbar = Scrollbar(root) 
scrollbar.pack( side = RIGHT, fill = Y ) 
mylist = Listbox(root, yscrollcommand = scrollbar.set ) 
for line in range(100): 
mylist.insert(END, 'This is line number' + str(line)) 
mylist.pack( side = LEFT, fill = BOTH ) 
scrollbar.config( command = mylist.yview ) 
mainloop() 


Output:

**14 - Text: **To edit a multi-line text and format the way it has to be displayed.

The general syntax is:

w  =Text(master, option=value)

There are number of options which are used to change the format of the text. Number of options can be passed as parameters separated by commas. Some of them are listed below.

  • highlightcolor: To set the color of the focus highlight when widget has to be focused.
  • insertbackground: To set the background of the widget.
  • bg: to set he normal background color.
  • font: to set the font on the button label.
  • image: to set the image on the widget.
  • width: to set the width of the widget.
  • height: to set the height of the widget.
from tkinter import *
root = Tk() 
T = Text(root, height=2, width=30) 
T.pack() 
T.insert(END, 'GeeksforGeeks\nBEST WEBSITE\n') 
mainloop() 


Output:

**15 - TopLevel: **This widget is directly controlled by the window manager. It don’t need any parent window to work on.The general syntax is:

w = TopLevel(master, option=value)

There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

  • bg: to set he normal background color.
  • bd: to set the size of border around the indicator.
  • cursor: To appear the cursor when the mouse over the menubutton.
  • width: to set the width of the widget.
  • height: to set the height of the widget.
from tkinter import *
root = Tk() 
root.title('GfG') 
top = Toplevel() 
top.title('Python') 
top.mainloop() 


Output:

**16 - SpinBox: **It is an entry of ‘Entry’ widget. Here, value can be input by selecting a fixed value of numbers.The general syntax is:

w = SpinBox(master, option=value)

There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

  • bg: to set he normal background color.
  • bd: to set the size of border around the indicator.
  • cursor: To appear the cursor when the mouse over the menubutton.
  • command: To call a function.
  • width: to set the width of the widget.
  • activebackground: To set the background when mouse is over the widget.
  • disabledbackground: To disable the background when mouse is over the widget.
  • from_: To set the value of one end of the range.
  • to: To set the value of the other end of the range.
from tkinter import *
master = Tk() 
w = Spinbox(master, from_ = 0, to = 10) 
w.pack() 
mainloop() 


Output:

17 - PannedWindowIt is a container widget which is used to handle number of panes arranged in it. The general syntax is:

w = PannedWindow(master, option=value)

master is the parameter used to represent the parent window.

There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

  • bg: to set he normal background color.
  • bd: to set the size of border around the indicator.
  • cursor: To appear the cursor when the mouse over the menubutton.
  • width: to set the width of the widget.
  • height: to set the height of the widget.
from tkinter import *
m1 = PanedWindow() 
m1.pack(fill = BOTH, expand = 1) 
left = Entry(m1, bd = 5) 
m1.add(left) 
m2 = PanedWindow(m1, orient = VERTICAL) 
m1.add(m2) 
top = Scale( m2, orient = HORIZONTAL) 
m2.add(top) 
mainloop() 


Output:

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.