Tkinter GUI Programming in Python
Tkinter GUI Programming in Python
in
Python
Introduction:
• A graphical user interface is an application that has buttons,
windows, and lots of other widgets that the user can use to
interact with your application.
Main Window
(400x300)
• tkinter also offers access to the geometric configuration of the
widgets which can organize the widgets in the parent windows.
1. pack () method:
The pack() method is used to organize components or widgets in
main window.
Syntax:
widget.pack (options)
Syntax:
widget.place(x,y)
Frame Acts like a container which can be used to hold the other widgets
Listbox Display the list items, The user can choose one or more items.
Scale Creates the graphical slider, the user can slide through the range of values
Toplevel Used to create and display the top-level windows(Open a new window)
Button Widget in Tkinter:
• The Button is used to add various kinds of buttons to the python
application. We can also associate a method or function with a button
which is called when the button is pressed.
Syntax: name = Button(parent, options)
The options are
• activebackground:It represents the background of the button when it is
active.
• activeforeground:It represents the font color of the button when it is active..
• bd: It represents the border width in pixels.
• bg: It represents the background color of the button.
• command:It is set to the function call which is scheduled when the function is
called.
• text: It is set to the text displayed on the button.
• fg: Foreground color of the button.
• height:The height of the button.
• padx:Additional padding to the button in the horizontal direction.
• pady:Additional padding to the button in the vertical direction.
• width:The width of the button.
Example: btndemo1.py
from tkinter import *
from tkinter import messagebox
top = Tk()
top.geometry("300x200")
def fun():
messagebox.showinfo("Hello", "Blue Button clicked")
The following method is associated with the Listbox to insert list item to listbox at
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 listboxdemo.py
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 options.
The Radiobutton is different from a checkbutton. Here, the user is provided with
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 is active.
• activeforeground:It represents the font color of the Radiobutton when when it is active.
• 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 is called.
• 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 the
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 different from
Entry because it provides a multi-line text field to the user so that the user can
write the text and edit the text inside it.