[英]How to assign user selected color to every element in a list in Python using tkinter
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 :我需要在 Python 中使用 tkinter 生成这样一个 GUI 窗口,它允许我执行以下操作:
PS the no. PS 没有。 of signals is dynamic, hence the color chooser buttons for each signal shall be dynamic too.信号是动态的,因此每个信号的颜色选择器按钮也应该是动态的。
The approach will be to use config()
.方法是使用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))
.您还可以使用elementsname.config(fg=(color))
更改元素的前景。
Here are some more links to websites which explain how to customize the Tkinter widgets:以下是更多网站链接,这些链接解释了如何自定义 Tkinter 小部件:
https://www.geeksforgeeks.org/how-to-add-a-border-color-to-a-button-in-tkinter/ https://www.geeksforgeeks.org/how-to-add-a-border-color-to-a-button-in-tkinter/
https://www.geeksforgeeks.org/python-tkinter-label/ https://www.geeksforgeeks.org/python-tkinter-label/
How to change the foreground or background colour of a Tkinter Button on Mac OS X? 如何在 Mac OS X 上更改 Tkinter 按钮的前景色或背景色?
https://www.tutorialspoint.com/python/tk_button.htm https://www.tutorialspoint.com/python/tk_button.htm
https://www.dummies.com/programming/python/using-tkinter-widgets-in-python/ https://www.dummies.com/programming/python/using-tkinter-widgets-in-python/
https://docs.python.org/3/library/tkinter.tix.html https://docs.python.org/3/library/tkinter.tix.html
Hope these help :)希望这些有帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.