Python GUI Programming-Practical Questions
1. Create 3 buttons named RED, BLUE and GREEN. Click on the buttons to change
background colour of window.
from tkinter import *
def redbg( ):
window.config(bg="RED")
def greenbg( ):
window.config(bg="GREEN")
def bluebg( ):
window.config(bg="BLUE")
window=Tk()
window.geometry("400x400")
b1=Button(window, text="RED" ,command=redbg)
b2=Button(window, text="GREEN" ,command=greenbg)
b3=Button(window, text="BLUE" ,command=bluebg)
b1.pack()
b2.pack()
b3.pack()
window.mainloop( )
2. Create a single line text input widget and a label. On clicking a button set the
label text as the text input through the entry widget.
from tkinter import *
def setlabel( ):
labeltext=e.get()
l.config(text=labeltext)
window=Tk()
window.geometry("400x400")
l=Label(window)
e=Entry(window,fg="RED",width=30)
b=Button(window, text="click" ,command=setlabel)
l.pack()
e.pack()
b.pack()
window.mainloop( )
3. Create an entry widget and input a text. On clicking on a button show the
entered text in a message box.
from tkinter import *
from tkinter import messagebox
def display():
txt=e.get()
messagebox.showinfo("Title",txt)
window=Tk()
window.geometry("400x400")
e=Entry(window,width=30)
b=Button(window,text="Click",command=display)
e.pack()
b.pack()
window.mainloop( )
4. Create a canvas and draw a straight line on it.
from tkinter import*
window=Tk()
window.geometry("400x400")
c=Canvas(window,bg="BLUE", height=300,width=400)
c.create_line(0,0,200,200,fill="RED")
c.pack()
window.mainloop( )
5.Write a program to display the text associated with a radio button in a message
box.
from tkinter import *
from tkinter import messagebox
def dis():
s=v.get()
messagebox.showinfo("title",s)
window = Tk()
window.geometry("400x400")
v=StringVar()
R1=Radiobutton(window,text="Male",variable=v,value="male")
R2=Radiobutton(window,text="Female",variable=v,value="female")
l=Label(window)
b=Button(window,text="Click",command=dis)
R1.pack()
R2.pack()
l.pack()
R2.invoke()
b.pack()
window.mainloop()
6.Write a program to trace the text associated with the selected check button(s)
from tkinter import *
def setval():
if (var1.get() == 1) and (var2.get() == 0):
l.config(text="You Love Music")
elif (var1.get() == 0) and (var2.get() == 1):
l.config(text="You Love Dance")
elif (var1.get() == 1) and(var2.get() == 1):
l.config(text="You Love Music&Dance")
else:
l.config(text="You Love neither" )
window = Tk()
window.geometry("400x400")
l = Label(window, width=20)
l.pack()
var1 =IntVar()
var2 = IntVar()
c1 = Checkbutton(window, text="Music",variable=var1, onvalue=1, offvalue=0,
command=setval)
c2 = Checkbutton(window, text="Dance",variable=var2, onvalue=1, offvalue=0,
command=setval)
c1.pack()
c2.pack()
window.mainloop()
7. Create a List box and add 3 buttons to add items to list,delete items from the list
and to print the list items.
from tkinter import *
def del1():
listbox.delete(ANCHOR)
def ins():
item=e.get()
listbox.insert(END,item)
def disp():
print(listbox.get(0,END))
window = Tk()
window.geometry("800x600")
lbl = Label(window,text = "A list of favourite countries..")
e=Entry(window)
e.pack()
listbox = Listbox(window,selectmode=EXTENDED)
listbox.insert(1,"India")
listbox.insert(2, "USA")
listbox.insert(3, "Japan")
#this button will delete the selected item from the list
btn1=Button(window, text = "delete",command=del1)
#this button will insert the item in the Entry to the end of listbox
btn2=Button(window, text = "insert",command=ins)
#this button will print a tuple containing all items in the list
btn3=Button(window, text = "display",command=disp)
lbl.pack()
listbox.pack()
btn1.pack()
btn2.pack()
btn3.pack()
window.mainloop()