Home > Mobile >  How do I change the background colour and foreground colour using a button in Tkinter?
How do I change the background colour and foreground colour using a button in Tkinter?

Time:07-08

So what I am trying to do is create a theme picker for my application

For example, the user could click on a the Green/Black button and it would change every widgets background to Black and it would change every widgets foreground to Green. Then they could click the Red/White button and it would do the same thing but change every widgets background to White and every widgets foreground to Red.

# Imports the tkinter library.
from tkinter import *
from tkmacosx import Button

# Pillow is a improved verison of PIL, stands for Python Image Library. Allows you to import image types like .jpg and .png
from PIL import ImageTk,Image

# Imports messagebox module from tkinter library.
from tkinter import messagebox

# Declare global variables
global selectedBackground
global selectedForeground

selectedBackground="white"
selectedForeground="red"

# Tk() helps to display the root window and manages all the other components of the tkinter application and assigns it to root.
root = Tk()
root.eval("tk::PlaceWindow . center")
root.configure(bg=selectedBackground)

cipherButton = Button(root, text="  Cipher  ", padx=40, pady=20, command=openCipher, borderwidth=0, fg="#22fd35", bg="black", highlightbackground="#22fd35").grid(row=1, column=0)
decipherButton = Button(root, text="Decipher", padx=40, pady=20, command=openDecipher, borderwidth=0, fg="#22fd35", bg="black", highlightbackground="#22fd35").grid(row=1, column=1)
spacer1 = Label(root, text="     ", padx=10, pady=1, background="black").grid(row=4, column=1)
quitButton = Button(root, text="Exit d3cryptt", padx=10, pady=5, command=root.quit, borderwidth=0, fg="#22fd35", bg="black", highlightbackground="#22fd35").grid(row=5, column=0, columnspan=2)
spacer2 = Label(root, text="     ", padx=10, pady=1, background=selectedBackground).grid(row=6, column=1)
changecolour = Button(root, text="change colour", padx=1, pady=5, background="black", command=changeColour).grid(row=7, column=0)

#Enter the event main loop
root.mainloop()

I have tried using a function. Something like this.

def changeColour():
    selectedBackground="black"
    selectedForeground="#22fd35"


changecolour = Button(root, text="change colour", padx=1, pady=5, background="black", command=changeColour).grid(row=7, column=0)

But that doesn't work. I think a function is the right way to go, but I may be wrong.

I will also need this 'theme' to carry on to any other windows that are created from this window. I think I can just do that by using lambda in the command section of the widget.

CodePudding user response:

Widget.config(bg=color) is what your looking for.

Here is a small example of a theme-changing app:

from tkinter import *
from tkmacosx import Button

root = Tk()


def changethemetoblack():
    root.config(bg="#000000")


def changethemetowhite():
    root.config(bg="#ffffff")


def changethemetored():
    root.config(bg="#ff0000")


themeblackbutton = Button(root, text="Change Theme To Black", command=changethemetoblack, bg="#000000", fg="#ffffff")
themewhitebutton = Button(root, text="Change Theme To White", command=changethemetowhite)
themeredbutton = Button(root, text="Change Theme To Red", command=changethemetored, bg="#ff0000", fg="#ffffff")

themeblackbutton.pack()
themewhitebutton.pack()
themeredbutton.pack()

root.mainloop()

I'll try to change it so that it applies for your code.
However, your provided script does not seem to be a working one. I assume this is because it is only a snippet of the real one. I'm not pushing for your entire code, but edit it so we can run it. Ex. openCipher method is causing errors as we have not defined it.

  • Related