-1

I am trying to plot signals on a graph. I need a feature where the list of signals selected by user for plotting is assigned a specific color chosen by the user himself. I need to generate such a GUI window using tkinter in Python which allows me to do :

  1. Show every element present in a list on one side.
  2. On the other side, a color chooser for the user to pick and choose color for that particular signal.

P.S. the no. of signals is dynamic, hence the color chooser buttons for each signal shall be dynamic too.

  • 1
    This is more like a software requisite, not a problem you are encountering. Are you willing to hire some developer? ^_^ Just kidding, this was to say that you need to provide information about what you tried, some code, etc. – piertoni Jul 28 '21 at 10:52

1 Answers1

0

The approach will be to use config(). You can change the color of an element using the following code:

from tkinter import *
from tkinter import colorchooser

#Creating the window and setting it's size
root = Tk()
root.geometry("500x500")

#Function to show the color chooser
def choosecol(*args):
    global colorchosen
    colorchosen = colorchooser.askcolor(title="Choose color")

#Function to change the background color of the label.
def changecol(*args):
    lbl.config(bg=colorchosen[1])

#Creating the label which will change it's color
lbl = Label(root, text="My color will change.")
lbl.place(x=300, y=200)

#Button which will show the color chooser when clicked
btn = Button(root, text="Click me", command=choosecol)
btn.place(x=200, y=200)

#Button which will change the color to the color chosen using the previous button
changebtn = Button(root, text="Change color", command=changecol)
changebtn.place(x=200, y=250)

#Starting the window
root.mainloop()

Like this, you can set the color of any element. You can also change the element's foreground using elementsname.config(fg=(color)).

Here are some more links to websites which explain how to customize the Tkinter widgets:

Nimantha
  • 6,405
  • 6
  • 28
  • 69
The Amateur Coder
  • 789
  • 3
  • 11
  • 33
  • I strongly advise against using wildcard (`*`) when importing something, You should either import what You need, e.g. `from module import Class1, func_1, var_2` and so on or import the whole module: `import module` then You can also use an alias: `import module as md` or sth like that, the point is that don't import everything unless You actually know what You are doing; name clashes are the issue. – Matiiss Jul 28 '21 at 12:39
  • @Matiiss Oh, I'm sorry. Does it mean the variables' names can't be the names of the classes or functions or variables in the Tkinter module (like Button() and askcolor()) or does it mean that the Tkinter functions like simply Button() or Label() won't work and only tkinter.Button() and tkinter.Label() will work? If just Button() won't work, in which cases will it not? Could you please explain or is there any documentation I can refer to? Should I edit the answer anywhere? Thanks for correcting the mistake! – The Amateur Coder Jul 28 '21 at 15:22
  • what? I was telling you that this: `from tkinter import *` is bad practice and should be avoided, what you should do instead is sth like `from tkinter import Tk, Button, Label` and then use as such: `Button(root, args*)` or import like this: `import tkinter` and in that case use as `tkinter.Button` or use an alias: import tkinter as tk and then use as: `tk.Button`, it is simple import stuff – Matiiss Jul 28 '21 at 15:24
  • now there is no problem, but for example you had sth like this: `from tkinter import *` and then `from tkinter.ttk import *`, the issue is that now both modules have imported a lot of classes that have the same name such as `Button`, `Label` and if you expected to use `tkinter` button, it could happen that you used `tkinter.ttk` button which has a bit different attributes and vice versa, the issue is that you may import sth and then it could get overwritten or may overwrite another thing and then it will be pretty hard to debug stuff – Matiiss Jul 28 '21 at 15:27
  • Oh ok. Now I get it. Thanks for explaining :D – The Amateur Coder Jul 28 '21 at 16:06