how to change background color using python on button click

how to change background color using python on button click,Tkinter,Change Background Color On Click
Share it:
how to change background color using python on button click



Introduction:

In these tutorials, we will learn how to change the background color using python on button click. This code will change the background color of the template on the button click.
The code uses the Tkinter module to create a layout and widgets that can call a specific python function. If the function is called it will launch a script that will dynamically change the background color with the name of the frame and dot notation config() set the value of color with the parameter bg="". 

We will be using Python programming language because it is widely used as an advanced-level programming language for a general technique to the developer.



Getting started:

First, you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.


Importing Modules

Then copy code that I provided below and paste it inside the IDLE text editor.


from tkinter import *



Setting up the Main Frame

we will now then create the mainframe for the application. To do that just copy the code below and paste it inside the IDLE text editor.
root = Tk()
root.title("Change Background Color On Click Using Python trickcode")
width = 600
height = 200
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
root.geometry("%dx%d+%d+%d" % (width, height, x, y))

root.resizable(0, 0)


Designing the Layout

#==============================FRAMES=========================================
Top = Frame(root, bd=2,  relief=RIDGE)
Top.pack(side=TOP, fill=X)
Form = Frame(root, height=200)
Form.pack(side=TOP, pady=20)

#==========LABELS=============================
lbl_title = Label(Top, text="Change Background Color On Click Using Python trickcode", font=('arial', 15))
lbl_title.pack(fill=X)



#=======BUTTON WIDGETS=============================
btn_red = Button(Form, text="Red", font=('arial', 20),command=changeToRed)
btn_red.grid(pady=0, row=0, column=0)
btn_blue = Button(Form, text="Blue", font=('arial', 20),command=changeToBlue)
btn_blue.grid(pady=0,  padx=10 ,row=0, column=1)
btn_green = Button(Form, text="Green", font=('arial', 20),command=changeToGreen)
btn_green.grid(pady=0,  padx=10 ,row=0, column=2)



Creating the Main Function


#=====METHODS================
def changeToRed():
    root.config(bg="red")
    Form.config(bg='red')

def changeToBlue():
    root.config(bg="blue")
    Form.config(bg='blue')

def changeToGreen():
    root.config(bg="green")
    Form.config(bg='green')


There you have it we just created a Change Background Color On Click using Python.
Share it:

python

Post A Comment:

1 comments:

  1. You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "https://codeprozone.com/code/cpp/32877/c-change-console-color.html" answered properly. Developers are finding an appropriate answer about c++ change console color related to the C++ coding language.

    ReplyDelete