TK Inter
TK Inter
In this Python class, we will discuss different interfaces that we can use to
develop a Python GUI (graphical user interface).
We may use one of the following alternatives for programming a Python GUI:
a. Tkinter : Python ships with the Tk GUI toolkit. Tkinter is an interface to
this.
b. PyQt : A Python binding of the cross-platform GUI toolkit Qt, PyQt is
implemented as a plug-in for Python.
c. wxPython : wxWidgets is a cross-platform GUI API for Python; wxPython is
a wrapper for this.
Python Tkinter
Python provides the standard library Tkinter for creating the graphical user
interface for desktop based applications.
Developing desktop based applications with python Tkinter is not a complex
task
import tkinter
m = tkinter.Tk()
m.mainloop()
There are two main methods used which the user needs to remember while
creating the Python application with GUI.
2. To change the name of the window, you can change the className to the
desired one.
3. The basic code used to create the main window of the application is:
m=tkinter.Tk()
mainloop():
2. mainloop() is an infinite loop used to run the application, wait for an event to
occur and process the event
m.mainloop()
Python Tkinter Geometry
The Tkinter geometry specifies the method by using which, the widgets are
represented on display.
1. Usually, when you create a gui using Tkinter, the size of the window
depends on the size and placement of components in the window.
2. But, you can also control the window size by setting a specific width and
height to the window.
3. You can set the size of Tkinter window by calling geometry() function on the
window with width and height string passed as argument.
Syntax – geometry()
To set a specific size to the window when using Python tkinter, use geometry()
function on the Tk() class variable.
#import statement
from tkinter import *
Observe that there is x between width and height in the argument provided to
geometry().
##### Note: Please note that window size width x height does not include the
window title.
gui = Tk()
gui.mainloop()
from tkinter import *
gui.mainloop()
- You can change that to any color based on your application’s requirement.
There are two ways through which you can change the background color of
window in Tkinter. They are:
In this example, we will use the full form background to set the background
color of Tkinter window.
#gui.configure(background='yellow')
gui.mainloop()
- tkinter provides different options to the button constructor to alter its look.
Example 3.1: Create Button using tkinter
from tkinter import *
gui = Tk(className='Python Examples - Button')
gui.geometry("500x200")
# create button
button = Button(gui, text='My Button', width=40, height=3, bg='blue',
fg='white')
# add button to gui window
button.pack()
gui.mainloop()
Example 3.2: How to Change Background Color of Tkinter Button during Mouse
Click?
You can change the button’s background color, while the button is pressed
using mouse, using activebackground property of the button.
l = Label(master, option=value)
- master is the parameter used to represent the parent window.
Example 4.2: Add text color and background color to Label using tkinter
from tkinter import *
gui = Tk(className='Python Examples - Label')
gui.geometry("500x200")
# create Label
l= Label(gui, text='Welcome to Python Class by K4U Infotech', fg="white",
bg="black", width=50, height=3)
# add Label to gui window
l.pack()
gui.mainloop()
gui.geometry("500x200")
e1 = Entry(gui).grid(row=0, column=1)
e2 = Entry(gui).grid(row=1, column=1)
gui.mainloop()
Example 5.2: Create and add color to entry field using tkinter
from tkinter import *
gui.geometry("500x200")
gui.mainloop()
gui.geometry("500x200")
gui.mainloop()
Example 6.2: Create text field with default text using tkinter
from tkinter import *
gui.geometry("500x200")
gui.mainloop()
cb = CheckButton(master, option=value)
- master is the parameter used to represent the parent window.
gui.geometry("500x200")
# create label
# create Listbox
Lb = Listbox(gui)
Lb.insert(1, 'Python')
Lb.insert(2, 'Java')
Lb.insert(3, 'C++')
Lb.insert(4, 'Any other')
gui.mainloop()
It is used to provide the user a option to select the appropriate choice exist
within the application.
mb = Menubutton(master, option=value)
- master is the parameter used to represent the parent window.
Example 9.1: Create Menubutton using tkinter
from tkinter import *
gui = Tk(className='Python Examples - Menubutton')
gui.geometry("500x200")
# create Menubutton
mb=Menubutton(gui,text="Cofee")
mb.menu=Menu(mb)
mb["menu"]=mb.menu
mb.menu.add_checkbutton(label='BlackCofee')
mb.menu.add_checkbutton(label='ColdCofee')
# add Menubutton to gui window
mb.pack()
gui.mainloop()
The top-level menus are the one which is displayed just under the title bar of
the parent window.
We need to create a new instance of the Menu widget and add various
commands to it by using the add() method.
m = Menu(master, option=value)
- master is the parameter used to represent the parent window.
It shows multiple choices to the user out of which, the user can select only one
out of them.
To keep track the user's selection the radiobutton, it is associated with a single
variable.
rb = RadioButton(master, option=value)
- master is the parameter used to represent the parent window.
gui.geometry("500x200")
# create Label
lb = Label(text = "Favourite programming language:")
lb.pack()
# create Radiobutton
v = IntVar()
rb1=Radiobutton(gui, text='Python', variable=v, value=1)
gui.mainloop()
It acts like a container which can be used to hold the other widgets.
The rectangular areas of the screen are used to organize the widgets to the
python application.
f = Frame(master, option=value)
- master is the parameter used to represent the parent window.
gui.geometry("500x200")
frame = Frame(gui)
frame.pack()
leftframe = Frame(gui)
leftframe.pack(side = LEFT)
rightframe = Frame(gui)
rightframe.pack(side = RIGHT)
bottomframe = Frame(gui)
bottomframe.pack(side = BOTTOM)
btn1 = Button(frame, text="Submit")
btn1.pack(side = LEFT)
btn2 = Button(frame, text="Remove")
btn2.pack(side = RIGHT)
btn3 = Button(rightframe, text="Add")
btn3.pack(side = LEFT)
btn4 = Button(leftframe, text="Modify")
btn4.pack(side = RIGHT)
btn5 = Button(bottomframe, text="Delete")
btn5.pack(side = BOTTOM)
gui.mainloop()