In: Computer Science
Python
Create a tkinter GUI application that has two buttons on it. The first button should have in red text the word "Red" on it and the second button should have in blue text the word "Blue" on it. When the red button is clicked you should change the background color of the application window to red and when the blue button is pressed you should change the background color of the application window to blue.
You should place your buttons on the window using the grid manager.
script.py
from tkinter import *
root = Tk() # Create root window for your application
root.geometry("150x150") # set the size of the main window
# Function to call when red btn is clicked
def colorRed():
''' Changes back color to red when red button is clicked'''
root.configure(background='red')
# Function to call when blue btn is clicked
def colorBlue():
''' Changes back color to blue when blue button is clicked'''
root.configure(background='blue')
# Creating buttons
redbtn = Button(root, text="Red", fg="red", command=colorRed)
bluebtn = Button(root, text="Blue", fg="blue", command=colorBlue)
# placing buttons on the window
redbtn.grid(row=1,column=1,padx=5, pady=5)
bluebtn.grid(row=1,column=2,padx=5, pady=5)
# Calling the mainloop on our root window
root.mainloop()
CODE:
OUTPUT:
when red button is clicked :-
when blue button is clicked:-