0% found this document useful (0 votes)
72 views16 pages

TK Inter

This document discusses Python GUI programming using Tkinter. It begins by describing Tkinter as the standard library module for building Python GUIs. It then discusses alternatives like PyQt and wxPython. The document proceeds to provide code examples for creating basic Tkinter apps using common widgets like buttons, labels, entries, and text fields. It also demonstrates how to set properties like window size, background color, and adding colors to widgets. Methods like pack, grid, place and mainloop are described. Overall the document serves as a tutorial on building Python GUIs with Tkinter.

Uploaded by

Honey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views16 pages

TK Inter

This document discusses Python GUI programming using Tkinter. It begins by describing Tkinter as the standard library module for building Python GUIs. It then discusses alternatives like PyQt and wxPython. The document proceeds to provide code examples for creating basic Tkinter apps using common widgets like buttons, labels, entries, and text fields. It also demonstrates how to set properties like window size, background color, and adding colors to widgets. Methods like pack, grid, place and mainloop are described. Overall the document serves as a tutorial on building Python GUIs with Tkinter.

Uploaded by

Honey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Python GUI Programming

In this Python class, we will discuss different interfaces that we can use to
develop a Python GUI (graphical user interface).

Alternatives For Python GUI Programming

Following are options for Python GUI Programming.

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

To create a tkinter app:


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.
NOTE :
1. Importing tkinter is same as importing any other module in the Python
code.
 import tkinter
2. Note that the name of the module in Python 2.x is ‘Tkinter’ and in Python
3.x it is ‘tkinter’.
Syntax :

import tkinter

m = tkinter.Tk()

''' widgets are added here '''

m.mainloop()

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

Tk(screenName=None, baseName=None, className=’Tk’, useTk=1):

1. To create a main window, tkinter offers a method ‘Tk(screenName=None,


baseName=None, className=’Tk’, useTk=1)’.

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()

where m is the name of the main window object

mainloop():

1. There is a method known by the name mainloop() is used when your


application is ready to run.

2. mainloop() is an infinite loop used to run the application, wait for an event to
occur and process the event

as long as the window is not closed.

m.mainloop()
Python Tkinter Geometry

The Tkinter geometry specifies the method by using which, the widgets are
represented on display.

The python Tkinter provides the following geometry methods.

1. The pack() method

2. The grid() method

3. The place() method

1. Python - Tkinter - Window

1.1 How to set Tkinter Window Size?

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 *

#create GUI Tk() variable


gui = Tk()

#set window size


gui.geometry("widthxheight")
where width and height should be replaced with integers that represent the
width and height of the window respectively.

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.

Example 1 : Set Window Size in Python tkinter


In this example, we will use geometry() method to set a fixed window size of 500
by 200 to the Tk() window.

from tkinter import *

gui = Tk()

# set window size


gui.geometry("500x200")

gui.mainloop()
from tkinter import *

gui = Tk(className='Python Examples - Window Size')

# set window size


gui.geometry("500x200")

gui.mainloop()

1.2 Tkinter Window Background Color

Set Tkinter Window Background Color


- The default background color of a GUI with Tkinter is "grey".

- 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:

using configure(bg='') method of tkinter.Tk class. or

directly set the property bg of tkinter.Tk.


In both of the above said cases, set bg property with a valid color value. You
can either provide a valid color name or a 6-digit hex value with # preceding
the value, as a string.

Example 2 : Change Background Color using -- configure method


In this example, we will change the Tkinter window’s background color to blue.

from tkinter import *


gui = Tk(className='Python Examples - Window Color')
# set window size
gui.geometry("400x200")
#set window color
gui.configure(bg='green')
gui.mainloop()

Example 3: Change Background Color using -- bg property


In this example, we will change the Tkinter window’s background color to blue.

from tkinter import *


gui = Tk(className='Python Examples - Window Color')
# set window size
gui.geometry("400x200")
#set window color
gui['bg']='green'
gui.mainloop()

Example 4: Change Background Color using -- background for bg


You can use the property name bg as in the above two examples, or also use
the full form background.

In this example, we will use the full form background to set the background
color of Tkinter window.

from tkinter import *


gui = Tk(className='Python Examples - Window Color')
# set window size
gui.geometry("400x200")
#set window color
gui['background']='yellow'

#gui.configure(background='yellow')
gui.mainloop()

Example 5: Using HEX code for background color


As already mentioned during the start, you can provide a HEX equivalent value
for a color.
In this example, we provide a HEX value to color and check the output.

from tkinter import *


gui = Tk(className='Python Examples - Window Color')
# set window size
gui.geometry("400x200")
#set window color
gui['background']='#856ff2'
gui.mainloop()

2. Python Tkinter Widgets


1 Button
2 Canvas
3 Checkbutton
4 Entry
5 Frame
6 Label
7 ListBox
8 Menubutton
9 Menu
10 Message
11 Radiobutton
12 Scale
13 Scrollbar
14 Text
15 Toplevel
16 Spinbox
17 PanedWindow
18 LabelFrame
19 MessageBox

3. Python tkinter - Button


When you are building a GUI application, button is one the building block that
makes your application interactive.

Syntax – tkinter Button


The syntax to add a Button to the tkinter window is:

mybutton = Button(master, option=value)


- Where master is the window to which you would like to add this button.

- 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.

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', activebackground='green', activeforeground='yellow')
# add button to gui window
button.pack()
gui.mainloop()
4. Python tkinter - 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.
Syntax – tkinter Label
The general syntax is:

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

Example 4.1: Create 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')
# add Label to gui window
l.pack()
gui.mainloop()

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()

5. Python tkinter - Entry Field


It is used to input the single line text entry from the user.

For multi-line text input, Text widget is used.

Syntax – tkinter Entry


The general syntax is:

entry = Entry(master, option=value)


- master is the parameter used to represent the parent window.

Example 5.1: Create entry field using tkinter¶


from tkinter import *

gui = Tk(className='Python Examples - Entry Field')

gui.geometry("500x200")

# create label field

l1=Label(gui, text='First Name').grid(row=0)


l2=Label(gui, text='Last Name').grid(row=1)

# create entry field

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 = Tk(className='Python Examples - Entry Field')

gui.geometry("500x200")

# create label field

l1=Label(gui, text='First Name').grid(row=0)


l2=Label(gui, text='Last Name').grid(row=1)

# create entry field

e1 = Entry(gui,fg="white", bg="light green", width=20).grid(row=0, column=1)


e2 = Entry(gui, fg="white", bg="light green", width=20).grid(row=1, column=1)

gui.mainloop()

6. Python tkinter - Text field


It is used to input the multi-line text entry from the user.

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


Syntax – tkinter Text Field
The general syntax is:

text = Text(master, option=value)


- master is the parameter used to represent the parent window.

Example 6.1: Create text field using tkinter


from tkinter import *

gui = Tk(className='Python Examples - Text Field')

gui.geometry("500x200")

# create Text field


t = Text(gui, height=5, width=20)

# add Text Field to gui window


t.pack()

gui.mainloop()

Example 6.2: Create text field with default text using tkinter
from tkinter import *

gui = Tk(className='Python Examples - Text Field')

gui.geometry("500x200")

# create Text field


t = Text(gui, height=5, width=50)

t.insert(END, 'Welcome to Python Class\nBy\nK4U Infotech\n')


t.pack()

gui.mainloop()

Example 6.3: Create label and text field using tkinter


from tkinter import *
gui = Tk(className='Python Examples - Entry Field')
gui.geometry("500x200")
# create label field
l1=Label(gui, text='First Name').grid(row=0)
# create Text field
t = Text(gui, height=5, width=50).grid(row=0, column=1)
gui.mainloop()

7. Python tkinter - Checkbutton


To select any number of options by displaying a number of options to a user as
toggle buttons.

Syntax – tkinter Checkbutton


The general syntax is:

cb = CheckButton(master, option=value)
- master is the parameter used to represent the parent window.

Example 7.1: Create checkbutton using tkinter


from tkinter import *
gui = Tk(className='Python Examples - Checkbutton')
gui.geometry("500x200")
# create checkbutton
var1 = IntVar()
cb1 = Checkbutton(gui, text='male', variable=var1)
var2 = IntVar()
cb2 = Checkbutton(gui, text='female', variable=var2)
# add Checkbutton to gui window
cb1.pack()
cb2.pack()
gui.mainloop()

8. Python tkinter - Listbox


It offers a list to the user from which the user can accept any number of
options.
### Syntax – tkinter Listbox
The general syntax is:

list = Listbox(master, option=value)


- master is the parameter used to represent the parent window.

Example 8.1: Create Listbox using tkinter


from tkinter import *

gui = Tk(className='Python Examples - Listbox')


gui.geometry("500x200")
# create Listbox
Lb = Listbox(gui)
Lb.insert(1, 'Python')
Lb.insert(2, 'Java')
Lb.insert(3, 'C++')
Lb.insert(4, 'Any other')
# add listbox to gui window
Lb.pack()
gui.mainloop()

Example 8.2: Create Label and Listbox using tkinter


# from tkinter import *

gui = Tk(className='Python Examples - Listbox')

gui.geometry("500x200")

# create label

l= Label(gui, text='Welcome to Python Class by K4U Infotech')

# create Listbox

Lb = Listbox(gui)

Lb.insert(1, 'Python')
Lb.insert(2, 'Java')
Lb.insert(3, 'C++')
Lb.insert(4, 'Any other')

# add listbox to gui window


l.pack()
Lb.pack()

gui.mainloop()

9. Python tkinter - Menubutton


The Menubutton widget can be defined as the drop-down menu that is shown
to the user all the time.

It is used to provide the user a option to select the appropriate choice exist
within the application.

The Menubutton is used to implement various types of menus in the python


application.
A Menu is associated with the Menubutton that can display the choices of the
Menubutton when clicked by the user.

### Syntax – tkinter Menubutton


The general syntax is:

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()

10. Python tkinter - Menu


The Menu widget is used to create various types of menus (top level, pull down,
and pop up) in the python application.

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.

### Syntax – tkinter Menu


The general syntax is:

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

Example 10.1: Create Menu using tkinter


from tkinter import *
gui = Tk(className='Python Examples - Menu')
gui.geometry("500x200")
# create Menu
menu = Menu(gui)
gui.config(menu=menu)
# create Menu - File
filemenu = Menu(menu)
menu.add_cascade(label='File', menu=filemenu)
filemenu.add_command(label='New')
filemenu.add_command(label='Open')
# create Menu - Help
helpmenu = Menu(menu)
menu.add_cascade(label='Help', menu=helpmenu)
helpmenu.add_command(label='About')
gui.mainloop()

11. Python tkinter - RadioButton


The Radiobutton widget is used to implement one-of-many selection in the
python application.

It shows multiple choices to the user out of which, the user can select only one
out of them.

We can associate different methods with each of the radiobutton.

We can display the multiple line text or images on the radiobuttons.

To keep track the user's selection the radiobutton, it is associated with a single
variable.

Each button displays a single value for that particular variable.


### Syntax – tkinter RadioButton
The general syntax is:

rb = RadioButton(master, option=value)
- master is the parameter used to represent the parent window.

Example 11.1: Create RadioButton using tkinter


from tkinter import *

gui = Tk(className='Python Examples - Radiobutton')

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)

rb2=Radiobutton(gui, text='C', variable=v, value=2)

rb3=Radiobutton(gui, text='Java', variable=v, value=3)


# add Radiobutton to gui window
rb1.pack(anchor=W)
rb2.pack(anchor=W)
rb3.pack(anchor=W)

gui.mainloop()

12. Python tkinter - Frame


Python Tkinter Frame widget is used to organize the group of widgets.

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.

### Syntax – tkinter Frame


The general syntax is:

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

Example 12.1: Create Frames using tkinter


from tkinter import *

gui = Tk(className='Python Examples - Radiobutton')

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()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy