d17 Lecture
d17 Lecture
d17 Lecture
• GUI stands for graphical user interface, a GUI is an interface that uses icons or other
visual indicators to interact with electronic devices, rather than only text via a command
line.
• For example, all versions of Microsoft Windows is a GUI, whereas MS-DOS is a
command line.
Tkinter
1. Tk was developed as a GUI extension for the Tcl scripting language by John Ousterhout.
2. The first release was in 1991. Tk proved as extremely successful in the 1990's, because it
is easier to learn and to use than other toolkits.
3. Tkinter is an inbuilt Python module used to create simple GUI apps.
4. It is the most commonly used module for GUI apps in the Python.
5. The name Tkinter comes from Tk interface.
window = tkinter.Tk()
window.title("GUI") # to rename the title of the window
# pack is used to show the object in the window
label = tkinter.Label(window, text = "Hello World!").pack( )
window.mainloop( )
1. Tkinter allows to access the geometric configuration of the widgets which can organize
the widgets in the parent windows.
2. There are mainly three geometry manager classes class.
• pack( ) method: It organizes the widgets in blocks before placing in the parent
widget.
• grid( ) method: It organizes the widgets in grid (table-like structure) before
placing in the parent widget.
• place( ) method: It organizes the widgets by placing them on specific positions
directed by the programmer.
Tkinter Widgets
Standard attributes
Common attributes of the widgets such as sizes, colors and fonts are specified.
• Dimensions
• Colors
• Fonts
• Anchors
• Relief styles
• Bitmaps
• Cursors
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 :
w=Label(master, option=value)
Example
w=Button(master, option=value)
• activebackground: to set the background color when button is under the cursor.
• activeforeground: to set the foreground color when button is under the cursor.
• bg: to set he normal background color.
• command: to call a function.
• font: to set the font on the button label.
• image: to set the image on the button.
• width: to set the width of the button.
• height: to set the height of the button.
import tkinter as tk
r = tk.Tk()
r.title('Counting Seconds')
button = tk.Button(r, text='Stop', width=25, command=r.destroy)
button.pack()
r.mainloop()
Example-2 for Button
import tkinter as tk
def show():
tk.Message(r,text="Hello", fg='red', font='Arial', bg='yellow', relief=tk.RAISED).pack()
return
r = tk.Tk()
r.title('Show Message')
button = tk.Button(r, text='Click Her
Here',
e', font='Arial', fg='brown', width=25, command=show)
button.pack()
r.mainloop()
Entry
It is used to input the single line text entry from the user.. For multi
multi-line
line text input, Text widget
is used.
Syntax
w=Entry(master, option=value)
• bd: to set the border width in pixels.
• bg: to set the normal background color.
• cursor: to set the cursor used.
• command: to call a function.
• highlightcolor: to set the color shown in the focus highlight.
• width: to set the width of the button.
• height: to set the height of the button.
Example
CheckButton
To select any number of options by displaying a number of options to a user as toggle buttons.
Syntax :
w = CheckButton(master, option=value)
RadioButton
It is used to offer multi-choice option to the user. It offers several options to the user and the user
has to choose one option.
• activebackground: to set the background color when widget is under the cursor.
• bg: to set he normal background color.
• command: to call a function.
• font: to set the font on the button label.
• image: to set the image on the widget.
• width: to set the width of the label in characters.
• height: to set the height of the label in characters.
Example
It acts as a container to hold the widgets. It is used for grouping and organizing the widgets.
Syntax :
w = Frame(master, option=value)
Example