[英]How to assign user selected color to every element in a list in Python using tkinter
我正在嘗試在圖表上繪制信號。 我需要一個功能,其中為用戶選擇用於繪圖的信號列表分配了用戶自己選擇的特定顏色。 我需要在 Python 中使用 tkinter 生成這樣一個 GUI 窗口,它允許我執行以下操作:
PS 沒有。 信號是動態的,因此每個信號的顏色選擇器按鈕也應該是動態的。
方法是使用config()
。 您可以使用以下代碼更改元素的顏色:
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()
像這樣,您可以設置任何元素的顏色。 您還可以使用elementsname.config(fg=(color))
更改元素的前景。
以下是更多網站鏈接,這些鏈接解釋了如何自定義 Tkinter 小部件:
https://www.geeksforgeeks.org/how-to-add-a-border-color-to-a-button-in-tkinter/
https://www.geeksforgeeks.org/python-tkinter-label/
如何在 Mac OS X 上更改 Tkinter 按鈕的前景色或背景色?
https://www.tutorialspoint.com/python/tk_button.htm
https://www.dummies.com/programming/python/using-tkinter-widgets-in-python/
https://docs.python.org/3/library/tkinter.tix.html
希望這些有幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.