his is the template in idle form # 3 labels, 3 radiobuttons (as per diagram) # I
ID: 3748346 • Letter: H
Question
his is the template in idle form
# 3 labels, 3 radiobuttons (as per diagram)
# Initially, the Labels are grey and display no text.
# When Radiobutton 1 is selected by the user, the first Label
# changes colour and displays the text "Ready..."
# When Radiobutton 2 is selected by the user, the second Label
# changes colour and displays the text "Set..."
# When Radiobutton 3 is selected by the user, the third Label
# changes colour and displays the text "...GO!"
#----------------------------------------------------------------
# Import the Tkinter functions
from tkinter import *
# Create a window
the_window = Tk()
# Give the window a title
the_window.title('Label Changer')
# PUT YOUR CODE HERE-------------------------------------------------#
# -------------------------------------------------------------------#
# Start the event loop
the_window.mainloop()
Explanation / Answer
# 3 labels, 3 radiobuttons (as per diagram) # Initially, the Labels are grey and display no text. # When Radiobutton 1 is selected by the user, the first Label # changes colour and displays the text "Ready..." # When Radiobutton 2 is selected by the user, the second Label # changes colour and displays the text "Set..." # When Radiobutton 3 is selected by the user, the third Label # changes colour and displays the text "...GO!" #---------------------------------------------------------------- # Import the Tkinter functions from tkinter import * def update(): x=val.get() #Taking appropriate action based on Radio Button selected rest of the code in this function in self explanatory if(x==1): a.config(text="Ready...",bg="red",font=('Helvetica','20')) b.config(text="",bg='gray') c.config(text="",bg='gray') if(x==2): b.config(text='Set...',bg="orange",font=('Helvetica','20')) a.config(text="",bg='gray') c.config(text="",bg='gray') if(x==3): c.config(text='...GO!',bg='green',font=('Helvetica','20')) b.config(text="",bg='gray') a.config(text="",bg='gray') # Create a window the_window = Tk() # Give the window a title the_window.title('Label Changer') #Setting up Labels on to the window a=Label(the_window,text="",padx=200,pady=30,bg="gray",relief="raised") a.grid(row=0,column=0,padx=10,pady=10) a.grid_propagate(False) b=Label(the_window,text="",padx=200,pady=30,bg="gray",relief="raised") b.grid(row=1,column=0,padx=10,pady=10) c=Label(the_window,text="",padx=200,pady=30,bg="gray",relief="raised") c.grid(row=2,column=0,padx=10,pady=10) #Intializing dictionary to assign values to radio buttons d1={1:'1',2:'2',3:'3'} val=IntVar() #Setting up radio buttons #Update function is written to perform appropriate action on radio button selection for i in d1: r=Radiobutton(the_window,text=d1[i],variable=val,value=i,command=update,font="10") r.grid(row=3,column=i) # Start the event loop the_window.mainloop()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.