Python Redefine the variable given as a parameter to the function

2024/11/18 19:59:12

Hello I am trying to make theme window with tkinter. There is 5 variable for different widgets's color. I will use color dialog for choosing colors but I don't want to define 5 functions. So I think I can make 1 function with 1 parameter of which variable to change color of it. My code is below;

My variables:

generalBgColor = '#454b4d'
generalFgColor = '#f2fdff'
generalBtnColor = '#0a5e7a'
mainLabelColor = '#ffffff'
highlightedLetterColor = '#ff0000'

Buttons that call pickColor function:

bgButton = tk.Button(themeWindow, text=' ', relief='ridge',bg=generalBgColor, width=6, command=lambda: pickColor(generalBgColor))fgButton = tk.Button(themeWindow, text=' ', relief='ridge',bg=generalFgColor, width=6, command=lambda: pickColor(generalFgColor))btnButton = tk.Button(themeWindow, text=' ', relief='ridge',bg=generalBtnColor, width=6, command=lambda: pickColor(generalBtnColor))mainlButton = tk.Button(themeWindow, text=' ', relief='ridge',bg=mainLabelColor, width=6, command=lambda: pickColor(mainLabelColor))letterButton = tk.Button(themeWindow, text=' ', relief='ridge',bg=highlightedLetterColor, width=6,command=lambda:pickColor(highlightedLetterColor))

And this is the my function:

def pickColor(variable):global generalBgColor, generalFgColor, generalBtnColor, mainLabelColor, highlightedLetterColortempColor = askcolor(color=variable, title='Choose A Color')variable = tempColor[1]

There is no error but variable doesn't change. I know this won't work this is the reason why I ask here for help.

Answer

tkinter natively supports color palette with tk_setPalette. Be aware that the name and value pairs are for tk.widgets only and doesn't support ttk.widgets. In addition one has to define the palette before creating any other widgets and the background option is a positional argument that must be set. According to the docs, the following options are currently supported:

> activeBackground
> foreground
> selectColor
> activeForeground  
> highlightBackground
> selectBackground
> background
> highlightColor    
> selectForeground
> disabledForeground    
> insertBackground  
> troughColor

a minimal exempel is given in the code below:

import tkinter as tkroot = tk.Tk()
root.tk_setPalette(background='#ff00f0',foreground='white')
tk.Label(root, text='test').pack()
root.mainloop()

Alternatively you always can specify a dictionary and like

my_options = {'background':'#000000'}

and unpack these in the constructor like tk.Label(root, **my_options)

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

Related Q&A

Need a Gui Keypad for a touchscreen that outputs a pin when code is correct

I have a raspberry pi with a touchscreen running raspbian, Im hoping to have a Gui on the touchscreen that had a number keypad that when a correct input is entered a pin will output to a door latch or …

How can i make the python to wait till i complete speaking?

I am writing a program to recognise the speech from a microphone and the code will process accordingly. The code I wrote for this purpose is below.import speech_recognition as sr import webbrowser impo…

Facing AttributeError: list object has no attribute lower

I have posted my sample train data as well as test data along with my code. Im trying to use Naive Bayes algorithm to train the model.But, in the reviews Im getting list of list. So, I think my code is…

why I cannot use max() function in this case? [duplicate]

This question already has answers here:Why do I get "TypeError: int object is not iterable" when trying to sum digits of a number? [duplicate](4 answers)Closed 1 year ago.n,m,k=map(int, inpu…

SQLALchemy and Python - Getting the SQL result

I am using cloudkitty which is rating module in OpenStacks.But here question is regarding the SQLAlchemy and Python.I am new to SQLAlchemy.I need to fetch some details from a table using a API call.So …

ValueError: invalid literal for int() with base 10: python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Closed 10 years ago.Questions asking for code must demonstrate a minimal understanding of the proble…

python code to connect to sftp server

I found this code to connect to remote sftp server with the help of username ,password and host but i also need to include the port number, can any one let em know how to include the port number in thi…

Python: get the max value with the location above and below than the max

If I have a dataframe like this, index User Value location1 1 1.0 4.5 2 1 1.5 5.23 1 3.0 7.04 1 2.5 7.55 2 1.0 11.56 2 1.…

Retrieve smart cards PAN with Python and pyscard

Im trying to retrieve the PAN of a smart card using pyscard in Python. What I have done so far is to connect to the reader and to retrieve various information about the reader and the card... but I can…

How to stop a specific key from working in Python

My laptop keyboard has a bug and it sometimes presses the number 5 randomly so i tried many things and they didnt work, I tried programming a code that can stop it but i couldnt because i am a beginner…