简体   繁体   English

如何使用 tkinter 将用户选择的颜色分配给 Python 列表中的每个元素

[英]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 窗口,它允许我执行以下操作:

  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.另一方面,一个颜色选择器供用户为该特定信号挑选颜色。

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.

相关问题 如何为列表中的每个元素分配 integer - How to assign an integer for every element in the list 如何使用 Tkinter 将网站 URL 分配为 Python GUI 中选定单选按钮的值? - How do I assign a website URL as the value of a selected radiobutton in a Python GUI using Tkinter? 如何按顺序为每个唯一元素分配编号,因为它出现在 python 的元素列表中 - How to assign number to every unique element in order as it appears in the list of elements in python Python Tkinter如何使用网格为窗口着色 - Python Tkinter how to color a window using grid Tkinter:为用户选择的点设置颜色 - Tkinter: set a color for points selected by the user 如何在 python 中使用 Tkinter 使用 Python 中的 Tkinter,使用一些选定颜色的迭代来更改 python 中的圆圈颜色,每次按下按钮都会改变颜色? - How to change a circles color in python, using iteration of a few selected colors, which changes with each button press, using Tkinter in python? 如何在python中为列表的每个元素指定一个名称 - How to assign a name to each element of a list in python Python:如何将列表或数组分配给数组元素? - Python:How to assign a list or an array to an array element? 如何为Pandas列中的每个元素分配一个列表(或系列)? - How can I assign a list (or series) to every element in column in Pandas? 如何分配 Python 列表元素? 语法错误:无法分配给 function 调用 - How to assign Python list element? SyntaxError: cannot assign to function call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM