4. GUI Technologies
Python provides many technology to create GUI.
Some are as following
Tkinter
Tkinter is used to create desktop application.
Wxpython
wxPython is a cross-platform toolkit.
Jpython
Jython is a Java implementation of Python
RTS Tech 4
5. Tkinter
Tkinter is inbuilt in python as an liabrary.
We can create desktop application using tkinter module.
Tkinter is easy to use.
Tkinter provides fast way to create GUI.
It is object based library to create GUI.
RTS Tech 5
6. How to get tkinter module
If not installed in python package then we can install it
using pip command.
pip install tkinter.
We can import tkinter module.
from tkinter import *
Or
import tkinter
RTS Tech 6
9. Create First GUI
Import tkinter module
Create main window using Tk.
Add widgets like buttons, Labels, Entry etc.
Call main event loop.
RTS Tech 9
11. Label widgets
This is used to show text on the window.
from tkinter import *
main=Tk()
label =Label(
text="Hello, World",
fg="white", # Set the text color to white
bg="black" , # Set the background color to black
font="bold", # Set the font
width=10,#set the width
height=3,#set the height
).pack()
main.mainloop()
RTS Tech 11
12. Button
Button is a label that we can click.
from tkinter import *
main=Tk()
button=Button(
text="Hello, World",
fg="white", # Set the text color to white
bg="black" , # Set the background color to black
font="bold", # Set the font
width=10,#set the width
height=3,#set the height
).pack()
main.mainloop()
RTS Tech 12
13. Get User input
We can take user input using entry widgets.
entry=Entry(
fg="black", # Set the text color to white
bg="Red" , # Set the background color to black
font="bold",
width=100,#set the width
).pack()
RTS Tech 13
14. Entry widgets Operation
entry.get()
To get user input
entry.delete()
To delete specific values of complete data
entry.insert()
To insert values.
RTS Tech 14
15. Entry Operations
entry=Entry(
fg="black", # Set the text color to white
bg="Red" , # Set the background color to black
font="bold",
width=100,#set the width
)
entry.insert(0,"Python")
name=entry.get()
print(name)
entry.delete(0)
entry.pack()
RTS Tech 15
16. Text Widgets
from tkinter import *
main=Tk()
text=Text(main).pack()
main.mainloop()
Text widgets are used to get multiline inputs
Text widgets has same methods as entry widgets.
RTS Tech 16
18. Get data from text widgets
Text widgets contains multiline input, so we can not just pass a
single index like Entry widgets.
We have to pass 2 arguments.
Line no: starts with 1.
Character index: starts with 0.
Index is passed as the string as “<lineNo>.<index>”.
To get 1st charcter of the 1st line
Print(text.get("1.0")).
To get all the data
Print(text.get("1.0“,tkinter.END)).
RTS Tech 18
19. Frame Widgets
It is a top level widget which contains other widgets.
It is used to organize the layout of the widgets.
It is used for the logical grouping of other widgets.
RTS Tech 19
28. Event Listeners
Events Description
<Button> A mouse button is pressed with the mouse
pointer over the widget
<FocusIn> Keyboard focus was moved to this widget,
<FocusOut> Keyboard focus was moved from this
widget to another widget
<Motion> The mouse is moved with a mouse button
being held down.
<Double Button> Similar to the Button event, see above, but
the button is double clicked instead of a
single click.
<key> Any key is pressed
RTS Tech 28
29. bind() function
Bind function is used to bind the event to the
mainloop
Bind takes 2 arguments
An Event type
An event handler Callback function name
For ex.
window.bind("<Button>",hello)
RTS Tech 29
35. Disclaimer
This is a educational Presentation to make
programming easier.
We have used images of different URLs to make
presentation better.
We respect the work of the owners of the URLs.
37. Disclaimer
This is an educational presentation to enhance the
skill of computer science students.
This presentation is available for free to computer
science students.
Some internet images from different URLs are used
in this presentation to simplify technical examples
and correlate examples with the real world.
We are grateful to owners of these URLs and
pictures.
RTS Tech 37