how to change root color using tkinter with a button command

62
October 12, 2021, at 03:30 AM
 import tkinter
from tkinter import *
import random
a = random.randint(1, 10)
print(a)
root = Tk()
root.geometry("500x500")
root.title(" peleg's random number")
bglightmode = "black"
fglightmode = "white"
root["bg"] = format(bglightmode)
wrongtxt = "Wrong answer, the number was {}".format(a)
righttxt = "Correct, the number was {}".format(a)

def Take_input():
    INPUT = inputtxt.get("1.0", "end-1c", )
    print(INPUT)
    if (INPUT == format(a)):
        Output.insert(END, righttxt)
    else:
        Output.insert(END, wrongtxt)

l = Label(text="pick a number between 1-10")
l["bg"] = "black"
l["fg"] = "white"
inputtxt = Text(root, height=10,
                width=35,
                bg=format(bglightmode),
                fg=format(fglightmode))
Output = Text(root, height=10,
              width=35,
              bg=format(bglightmode),
              fg=format(fglightmode))
b1 = Button(root, height=2,
            bg=format(bglightmode),
            fg=format(fglightmode),
            width=20,
            text="Show",
            command=lambda: Take_input())
b2 = Button(root, height=2,
            width=20,
            text="exit",
            bg=format(bglightmode),
            fg=format(fglightmode),
            command=root.destroy)
b3 = Button(root, height=2,
            width=20,
            text="ready",
            bg=format(bglightmode),
            fg=format(fglightmode),
            command = if bglightmode)

 l.pack()
inputtxt.pack()
Output.pack()
b1.pack()
b2.pack()
b3.pack()
mainloop()

the main problem is here with this part of the code:

b3 = Button(root, height=2,
            width=20,
            text="ready",
            bg=format(bglightmode),
            fg=format(fglightmode),
            command = if bglightmode)

i tryed fixing it but was unable, does anyone know the solution? by the way im a beginner so sorry for the stupid question also what im trying to do is a toggle button between light and dark mode i thought making a switch would be the easiest way but if there is an easier way please comment

i was wanting to do this

b3 = Button(root, height=2,
            width=20,
            text="ready",
            bg=format(bglightmode),
            fg=format(fglightmode),
            command = if bglightmode == "black": 
......

but because its in a button command i cant

READ ALSO
Problem on Safari - Hide Header on Scroll Down & Show on Scroll Up

Problem on Safari - Hide Header on Scroll Down & Show on Scroll Up

It works on Chrome and other browsers but does not work on Safari

33
Can I unarchive .aar, then change string resources and archive back?

Can I unarchive .aar, then change string resources and archive back?

I have aaar library file and want to change the string value in res/values

39
MySQLdb.cursor.execute can't run multiple queries

MySQLdb.cursor.execute can't run multiple queries

We're trying to run SQL files containing multiple insert statements as a single query, but it seems rollback fails when any of the statements contain an error

63
java.lang.ClassNotFoundException HOW DO I SOLVE THIS [closed]

java.lang.ClassNotFoundException HOW DO I SOLVE THIS [closed]

Want to improve this question? Add details and clarify the problem by editing this post

35