GUI Programming Using Tkinter
GUI Programming Using Tkinter
in
Python
Introduction:
• A graphical user interface is an application that has butt
windows, and lots of other widgets that the user can us
interact with your application.
Main Windo
(400x300)
• tkinter also offers access to the geometric configuration
widgets which can organize the widgets in the parent w
1. pack () method:
The pack() method is used to organize components or w
main window.
Syntax:
widget.pack (options)
Syntax:
widget.place(x,y)
Frame Acts like a container which can be used to hold the other widg
Listbox Display the list items, The user can choose one or more items.
Scale Creates the graphical slider, the user can slide through the rang
def fun():
messagebox.showinfo("Hello", "Blue Button clicked")
The following method is associated with the Listbox to insert list item to
specified index.i.e, insert ().
Syntax:
Listbox.insert (index, item)
Example: listboxdemo.py
from tkinter import *
top = Tk()
top.geometry("300x200")
lbl1 = Label(top, text="List of Colours",fg="red",bg="yellow")
lbl1.place(x=10,y=10)
lb = Listbox(top,height=5)
lb.insert(1,"Red")
lb.insert(2, "Yellow")
lb.insert(3, "Green")
lb.insert(4, "Blue")
lb.place(x=10,y=30)
lbl2 = Label(top, text="List of Fruits",fg="blue",bg="green") Output:
lbl2.place(x=160,y=10) >>>python listboxde
lb1 = Listbox(top,height=5)
lb1.insert(1,"Mango")
lb1.insert(2, "Grapes")
lb1.insert(3, "Banana")
lb1.insert(4, "Berry")
lb1.place(x=160,y=30)
top.mainloop()
! Radiobutton Widget in Tkinter:
• The Radiobutton widget is used to select one option among multiple
The Radiobutton is different from a checkbutton. Here, the user is pr
various options and the user can select only one option among them
Syntax: name = Radiobutton(parent, options)
The options are
• activebackground:It represents the background of the Radiobutton when it
• activeforeground:It represents the font color of the Radiobutton when whe
• bd: It represents the border width in pixels.
• bg: It represents the background color of the Radiobutton.
• command:It is set to the function call which is scheduled when the function
• text: It is set to the text displayed on the Radiobutton.
• fg: Foreground color of the Radiobutton.
• height:The height of the Radiobutton.
• padx: Additional padding to the Radiobutton in the horizontal direction.
• pady: Additional padding to the Radiobutton in the vertical direction.
• width:The width of the Radiobutton.
• Variable: It is used to keep track of the user's choices. It is shared among all
radiobuttons.
Example: rbtndemo.py
from tkinter import *
top = Tk()
top.geometry("200x100")
radio = IntVar()
rbtn1 = Radiobutton(top, text="red",variable=radio,value="1")
rbtn1.pack()
rbtn2 = Radiobutton(top, text="Green",variable=radio,value="2")
rbtn2.pack()
rbtn3 = Radiobutton(top, text="Blue",variable=radio,value="3")
rbtn3.pack()
top.mainloop()
Output:
>>>python rbtndemo.py
! Text Widget in Tkinter:
• The Text widget allows the user to enter multiple lines of text.It is dif
Entry because it provides a multi-line text field to the user so that th
write the text and edit the text inside it.