0

Is there a way i can change the background color of tkinter from different modules?

For example lets say Simpletoggle runs. within Simpletoggle there is a another function that gets called, this function from the different file should change the color of the background.

The idea is to have colour effect when a different function is called.

Thank you

First module

def Simpletoggle():
    data()
      
 

ws = Tk()
ws.title("Python Guides")
app_picture = Label (ws,image=filename)
toggle_button = Button(text="Active", width=10, command=Simpletoggle)
ws.configure(bg='red')
ws.mainloop()

1 Answers1

0

Here's a really simple example where the function to change button colour is taken from another file, in this case Seconday, and used in simpletoggle to configure whatever widget is assigned as an input, in this case the root window, ws.

Main.py

from tkinter import *
from Secondary import update_colour

ws = Tk()
ws.title("Python Guides")


def simpletoggle():
    # data()
    update_colour(ws)


app_picture = Label(ws)
app_picture.grid(column=0, row=0)

toggle_button = Button(ws, text="Active", command=simpletoggle)
toggle_button.grid(column=0, row=0)

ws.configure(bg='red')


ws.mainloop()

Secondary.py

def update_colour(widget):
    widget.configure(bg='green')
DWR Lewis
  • 470
  • 4
  • 15
  • thank you! Will i have to repeat what has been done in secondary.py for other modules? – new_programmer11 Jan 17 '22 at 21:33
  • I'm not quite sure what you mean without knowing what specifically you are trying to do with your other tkinter widgets. If its just changing colour you could just use `update_colour` and change the widget input. I'd advocate formatting tkinter using a class structure though, as it can make passing variables and accessing tkinter widgets far easier. [Heres a link on tkinter classes](https://stackoverflow.com/a/17470842/12282048), and a [really helpful response](https://stackoverflow.com/a/70614538/12282048) to a question I asked recently about accessing variables & widgets in other classes. – DWR Lewis Jan 17 '22 at 22:02