Skip to main content

Change Background Color On Click Using Python Source Code

Language


In this tutorial we will create a Change Background Color On Click using Python. This code will change the background color of the template when user click the button. The code use tkinter module to create a layout and widgets that can call a specific python functions. 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 advance-level programming language for a general technique to the developer. Beginners find Python a clean syntax and indentation structure based, and it is easy to learn because of its less semi colon problem.

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

After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python.

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

  1. from tkinter import *

Setting up the Main Frame

After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.

  1. root = Tk()
  2. root.title("Change Background Color On Click Using Python Source Code")
  3. width = 600
  4. height = 200
  5. screen_width = root.winfo_screenwidth()
  6. screen_height = root.winfo_screenheight()
  7. x = (screen_width/2) - (width/2)
  8. y = (screen_height/2) - (height/2)
  9. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  10. root.resizable(0, 0)

Designing the Layout

After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.

  1. #==============================FRAMES=========================================
  2. Top = Frame(root, bd=2, relief=RIDGE)
  3. Top.pack(side=TOP, fill=X)
  4. Form = Frame(root, height=200)
  5. Form.pack(side=TOP, pady=20)
  6.  
  7. #==============================LABELS=========================================
  8. lbl_title = Label(Top, text="Change Background Color On Click Using Python Source Code", font=('arial', 15))
  9. lbl_title.pack(fill=X)
  10.  
  11.  
  12.  
  13. #==============================BUTTON WIDGETS=================================
  14. btn_red = Button(Form, text="Red", font=('arial', 20),command=changeToRed)
  15. btn_red.grid(pady=0, row=0, column=0)
  16. btn_blue = Button(Form, text="Blue", font=('arial', 20),command=changeToBlue)
  17. btn_blue.grid(pady=0, padx=10 ,row=0, column=1)
  18. btn_green = Button(Form, text="Green", font=('arial', 20),command=changeToGreen)
  19. btn_green.grid(pady=0, padx=10 ,row=0, column=2)

Creating the Main Function

This is where the code that contains the main function of the application. This code will change the background color when the button is clicked. To do that just copy and write these blocks of code.

  1. #==============================METHODS========================================
  2. def changeToRed():
  3. root.config(bg="red")
  4. Form.config(bg='red')
  5.  
  6. def changeToBlue():
  7. root.config(bg="blue")
  8. Form.config(bg='blue')
  9.  
  10. def changeToGreen():
  11. root.config(bg="green")
  12. Form.config(bg='green')

There you have it we just created a Change Background Color On Click using Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

 

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.