0

I want to be able to change the background colour of a Tkinter frame in a thread, the frame is declared in a separate function. I receive the following error when I run the following code.

Error: NameError: name 'mainScreen' is not defined

Code:

import tkinter as tk
from tkinter import ttk
from multiprocessing import Process


def main():
    global mainScreen
    
    root = tk.Tk()
    root.geometry('1040x540+50+50')

    mainScreen = tk.Frame(root, width = 1040, height = 540)
    mainScreen.place(x=0, y=0)

    root.mainloop()


def test(): # This function is in a thread as it will be run as a loop.
    while True:
        mainScreen.configure(bg='red')

if __name__ == '__main__':
    p2 = Process(target = test)
    p2.start()
    main()

Any help is appreciated.

2
  • You're starting the thread before you create the screen. Have you tried creating the screen before starting the thread? Commented Sep 25, 2020 at 17:17
  • If all you're doing is changing the color of a widget, threads are not at all necessary. Commented Sep 25, 2020 at 17:18

1 Answer 1

1

You can replace your whole code with this:

import tkinter as tk

def main():
    global mainScreen

    root = tk.Tk()
    root.geometry('1040x540+50+50')

    mainScreen = tk.Frame(root, width=1040, height=540)
    mainScreen.place(x=0, y=0)
    mainScreen.configure(bg='red')
    root.mainloop()


if __name__ == '__main__':
    main()

And if you want to change colours you can do something like this:

import time
import tkinter as tk
from threading import Thread


def test(mainScreen):  # This function is in a thread as it will be run as a loop.
    while True:
        try:
            time.sleep(1)
            mainScreen.configure(bg='red')
            time.sleep(1)
            mainScreen.configure(bg='blue')
        except RuntimeError:
            break


if __name__ == '__main__':
    root = tk.Tk()
    root.geometry('1040x540+50+50')

    mainScreen = tk.Frame(root, width=1040, height=540)
    mainScreen.place(x=0, y=0)
    p2 = Thread(target=test, args=(mainScreen,))
    p2.start()

    root.mainloop()
Sign up to request clarification or add additional context in comments.

4 Comments

I know, but I'm making a loop to change the colour of the background.
@ultimateduc Edited answer so you can use the while loop as well
Doesn't work as I cant assign colours in the test function.
@ultimateduc Fixed the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.