Tkinter is a Graphical User Interface (GUI) package. It is used to create interactive user interface by adding widgets to the application.
The Tkinter package comes pre-installed in Python.
In the case that it is not installed open command prompt/terminal and type:
~pip install tkinter
Tk(screenname = None, baseName =Name, class_ame = ‘Tk’ useTk=1)
To create a main window, Tkinter offers this method. To change the name of the window, you can change the class_name.
mainloop()
mainloop()
is an infinite loop used to run the application infinitely and it waits for an event, such as closing the application, to stop the loop.
Import tkinter
From Tkinter import everything (*).
Create a variable to store the body of the GUI.
To add the title use root.title(‘title’) function
To run the app and close the body use mainloop()
function.
1 2 3 4 5 6 7 8
import tkinter as tk from tkinter import * root = tk.Tk() root.title("First App") #body of the GUI root.mainloop()
Import tkinter.
To specify the geometry use root.geometry(‘dimension’) function.
To give a background colour use
1 2 3
root.config(bg = ’color_name / hex_color_code’) root[‘background’] = ’color_name / hex_color_code’ root[‘bg’] = ’color_name / hex_color_code’
Close the body with root.mainloop()
.
1 2 3 4 5 6 7 8
import tkinter as tk root = tk.Tk() root.title("First App") root.geometry('300x300') root.config(bg = 'gray') root.mainloop()
The button widget is used to add a button. Buttons can display text or images and use the command function to call the function whenever the button is pressed.
Import tkinter
Import messagebox
Define a function, here dialogueBox()
.
Inside the function display a pop-up window use messagebox.showinfo()
Inside messagebox.showinfo()
pass the title and the text of the pop-up.
To create a button widget use Button(‘text_of_button’,command = function_name).pack()
pack()
is used to close the body of any widget.
We can show / ask various types of pop-messages like:
1
2
3
4
5
6
7
showinfo()
showwaning()
showerror()
askquestion()
asktocancel()
askyesno()
askretrycancel()
1 2 3 4 5 6 7 8 9 10 11 12 13
import tkinter from tkinter import messagebox root = Tk() root.geometry('100x100') def box(): messagebox.showinfo('Title', 'Hello World!') tkinter.Button(root, text = "Click me", command = box) .pack() root.mainloop()
Result:
Import tkinter.
From tkinter import Label and Text.
Create a window using mainloop()
.
To add a label widget use Label(window_name, options)
.
Options such as text, bg, height, width etc.
To place a widget to a specific place we use grid(row =row_value ,column= column_value)
.
Create an entry box using Entry(window_name)
widget.
1
2
3
4
5
6
7
8
9
10
11
12
13
import tkinter as tk
from tkinter
import *
root = tk.Tk()
root.geometry('200x200')
Label(root, text = 'Username ')
.grid(row = 0, column = 0)
Entry(root)
.grid(row = 0, column = 1)
root.mainloop()
Creating a simple login app.
Import tkinter.
From tkinter import messagebox, Label, Button, Entry.
Create two labels and two entry boxes.
Create a function showData()
to fetch and display the dialoguebox.
To fetch the data from the Entry field use get()
function.
Pass get()
function to showinfo()
function.
Create a button login and set the command parameter to showData().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import tkinter as tk
from tkinter
import Label, Entry, Button, messagebox
def showData():
textName = name.get()
messagebox.showinfo('Logged in', textName + " logged in successful")
root = tk.Tk()
root.geometry('200x200')
Label(root, text = 'Username ')
.grid(row = 0, column = 0)
Label(root, text = 'Password ')
.grid(row = 1, column = 0)
name = Entry(root)
name.grid(row = 0, column = 1)
password = Entry(root, show = '*')
password.grid(row = 1, column = 1)
Button(root, text = "Login", command = showData)
.grid(row = 2, column = 0, columnspan = 2)
root.mainloop()
Result: