Why does my Tkinter window background not change?

2024/10/5 19:20:51

I've a small program with a feature to change the background color of a different window than the frame I use to ask for the background color. (I hope you understand this.) The program is written in Python with the Tkinter library.

Every time I click Enable in the window it changes the color of the background in the second window but not to the color I choose.

Where is the problem?

def Options():
def OptionButton():c= wert.get()if c == 1:fenster.config(background="red")elif c == 2:fenster.config(background="blue")else:fenster.config(background="yellow")global usermodusermod= tkinter.Entry.get(opEntry)fenster.update()option.destroy()def close():option.destroy()option=tkinter.Tk()
option.title("Options")
option.config(background="white")#Backgroundframe
bgFrame= tkinter.Frame(option)
bgFrame.grid(row=0, column=0, padx=10, pady=20)info3=tkinter.Label(bgFrame,text="Hintergrund im Menü")
info3.grid(row=1,column=0,padx=10, pady=20)#2 Radiobuttonglobal wert
wert = tkinter.StringVar()knopfA=tkinter.Radiobutton(bgFrame,text="Red", variable=wert,value="1")
knopfB=tkinter.Radiobutton(bgFrame,text="Blue", variable=wert, value="2")
knopfC=tkinter.Radiobutton(bgFrame,text="green", variable=wert,value="3")knopfA.grid(column=0,row=2)
knopfB.grid(column=0,row=3)
knopfC.grid(column=0,row=4)knopfA.select()#OptionFrame
opFrame = tkinter.Frame(option)
opFrame.grid(row=0, column=1,padx=10,pady=20)#Optionbutton
oplabel=tkinter.Label(opFrame,text="Value User Mode")
oplabel.grid(row=1,column=0,padx=10,pady=2)opEntry=tkinter.Entry(opFrame,width=12)
opEntry.grid(row=2,column=0,padx=10,pady=2)#OptionButtonFrame
btFrame=tkinter.Frame(option)
btFrame.grid(row=3,column=0,padx=10,pady=20)opbutton=tkinter.Button(btFrame,text="Enable", command = lambda:OptionButton())
opbutton.grid(row=1, column=0,padx=10,pady=2)clbutton=tkinter.Button(btFrame,text="close",command=lambda:close())
clbutton.grid(row=2, column=0,padx=10,pady=2)option.mainloop()
Answer

The problem is this code:

c = wert.get()if c == 1:fenster.config(background="red")
elif c == 2:fenster.config(background="blue")
else:fenster.config(background="yellow")

The .get() method is returning a str, because you declared wert to be a StringVar() but you're comparing to an int. Either convert via int(c) or compare strings:

if c == '1':

or perhaps declare wert to be an IntVar(). Also, reread how the Python global statement works -- use it where you want to modify a global within a function, not where the global is defined.

https://en.xdnf.cn/q/119181.html

Related Q&A

Combining a nested list without affecting the key and value direction in python

I have a program that stores data in a list. The current and desired output is in the format:# Current Input [{Devices: [laptops, tablets],ParentCategory: [computers, computers]},{Devices: [touch], Par…

Splitting very large csv files into smaller files

Is Dask proper to read large csv files in parallel and split them into multiple smaller files?

How to make this Python script to run subfolders too?

Which part of the codes do I need to change in order to include subfolders? File handle.py import glob import os import sys from typing import Listdef get_filenames(filepath: str, pattern: str) -> …

Why doesnt this recursive GCD function work? [duplicate]

This question already has answers here:Why does my recursive function return None?(4 answers)What is the purpose of the return statement? How is it different from printing?(15 answers)Closed 1 year …

How can I append \n at the end of the list in list comperhansion

I have this code:def table(h, w):table = [[. for i in range(w)] for j in range(h)]return tablewhich returns this[ [., ., .], [., ., .], [., ., .], [., ., .] ]How to make it return this?[[., ., .],[., …

Seaborn bar plot y axis has different values than expected

The y values in the Seaborn barplot are different than what my data shows.My data shows:yearmonth 2018-10 763308.0 2018-11 708366.0 2018-12 703952.0 2019-01 844039.0 2019-02 749583.…

Merge multiple JSON into single one (Python)

I am searching for a way to merge multiple JSONs into a single one. My output is in this format:[{"Nome bollettino": "Bollettino 1"}, {"Causale": "1"}, {"Nu…

I need to filter contents of my text file

I have a text file that I want to loop through, slice some contents, and store in a separate list. The text file contains:blu sre before we start start the process blah blah blah blha end the process b…

How to remove a number from a list that has a range between two numbers? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 6 years ago.Improve…

How do I solve an attribute error?

So like I said before my code (Or my current project that I am working on) is riddled with errors. So far I have at least solved a dozen errors or more and honestly I just give up. I mean God knows how…