banner



How To Create A Gui In Python Using Tkinter

10 Interesting Python Tkinter Programs with Code

All the programs explained with code in detail

Swathi Arun

Photo by Christopher Gower on Unsplash

Tkinter is one of the best modules to use in the Python programming language. It is specifically used to create a Graphical User Interface. With this module, we can create some fantastic programs. In this article, we try to create a simple yet interesting program. We are specifically going to focus on simple GUI to understand Tkinter better.

1. A Digital Clock with Tkinter:

A program to create a digital clock with Tkinter to display the time.

In this program, we use two packages: time and Tkinter. By using the time package we display the local time with the help of the strftime function.

Output:

Note:

after() — is used with an attribute of 1000 milliseconds that is after 1 second the clock will refresh and this makes it look like a running clock.

config — Change the property of the widget after its initialization. For example, in the code, we add time text to the window. Attributes in the function( %I,%M,%S) represent hours, minutes, and seconds local time respectively.

2. Change color Background with Tkinter:

Create a program that has a background with changing colors

This program has two packages to change the screen's background color in a random method. To import the random method we can use import random or directly you can import the randint function as done in the program.

Output:

Note:

geometry — In the Tkinter geometry method is used to set the dimension of the window. In the program, we have mentioned the height and width as 400 by 400.

title — The title method is used to name the root window. By default, it is tk to overwrite we use this method. Notice that I have used this method to name the window as color.

3. To-Do List with Tkinter:

Create a program to create a to-do list presented with Graphical User Interface(GUI) and Tkinter module.

This program creates a simple GUI. This simple program will contain the list supplied by you as a to-do list. This requires a Tkinter module that can be imported using from tkinter import * syntax. This syntax just means that you import all methods, variables in the Tkinter module should be imported.

Output:

Note:

pack() — is one of the three geometry managers of Tkinter that is used to organize the widgets present in the window.

Listbox()— This is a widget used to display a list of items. to this notice that there are two attributes Listbox(parent,options). The parent attribute is nothing but a window the list is displayed and options attributes can be used to enhance like background color, height, font, and many other options are available.

4. Display an alert message:

A program to display an alert message when a button is pressed.

We try to write a program to show an alert on an event occurrence. We first import the message box module from Tkinter to display this message. In our program, we just display a warning if the button is clicked.

Output:

Note:

place() — Similar to the pack function, the place is also used to organize any widget. One difference is that with the place you can set the coordinates of the widget as required.

messagebox — This module has different objects. In the program, I have used messagebox.showwarning(). Try different objects like askyesno(), show error().

5. A Tkinter script to shutdown PC:

Write a Python program with a shutdown button to shut down your computer.

In this program, we have used an OS package, which is used to shut down the computer. With this package operations like restart, shutdown can be performed. To install this package use import os syntax.

Note:

  • Before executing this program remember to close and save all the files to avoid data loss.
  • command — This is used to call functions on certain events like mouse-click or keypress. It binds these events and the call of the function. In the program, when the button shut down is clicked it calls the function shut( ).

6. Generate a simple password:

Create a program to generate a random password with upper and lower case letters.

In this program, we have used packages like random and string to generate passwords. We have written a program to generate eight-letter passwords with upper and lower case letters.

Output:

Note:

button, label —These are widgets used in the program. In this program, the button is named generate and the label displays the password.

7. Capitalize letters with Tkinter:

A simple program that takes input and change letter to upper case.

We have created a simple GUI to take input letters and change this into upper case letters in this program.

Output:

Note:

text — A text widget is used to display different kinds of text in a Tkinter application.

8. A simple Wikipedia search app:

A program to create an app with which you get information from Wikipedia

With this program, we try to implement a search application. In this application we search for data with Wikipedia, to import this we use import wikipedia syntax.

Output:

Note:

frame — This is a widget that can be used to organize other widgets in your application. With the customize all the widgets with the frame widget options available.

9. Add image to application:

Create a program with which you can add an image to the Tkinter application.

With this program, we are trying to create a program to add images to a GUI. We upload the image with the Python Imaging Library(PIL) Python package. We have used two packages Image( ) to open the image and ImageTk( ) to load the image.

Output:

Note:

PhotoImage — This object is used to load the image to the Tkinter application. In the program, we open the photo with the Image object, and then this is uploaded with the PhotoImage object.

mainloop — This event-driven method is used in all the Tkinter programs. This window waits for any event that occurs until the window is closed like a button click.

10. An age calculator application:

Create a program to take input of your date of birth and output your age.

A simple application to calculate your age using the given input. In this application, the date of birth provided is subtracted from the package datetime . The obtained number of days is converted to years and output is displayed

Output:

One of the Best Performing Article From The Author:

Do consider subscribing through my link to Medium. This will help, support and encourage. Thank you

How To Create A Gui In Python Using Tkinter

Source: https://levelup.gitconnected.com/10-interesting-python-tkinter-programs-with-code-df52174993e1

Posted by: milliganmolithery.blogspot.com

0 Response to "How To Create A Gui In Python Using Tkinter"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel