2022年11月17日 星期四

Python GUI : 選擇按鈕的背景顏色 Selecting background colour for buttons

選擇按鈕的背景顏色 Selecting background colour for buttons

import tkinter as tk

from tkinter import colorchooser

def collect_color(k):

    c_code = colorchooser.askcolor(title ="Select color")

    buttons[k].config(bg=c_code[1])

     

my_w = tk.Tk()

my_w.geometry("410x250")  # Size of the window 


n=20 # number of buttons

i=2 # row 

j=0 # column 

buttons = []

for k in range(n):

    e = tk.Button(my_w, text=k,height=2,width=10,

command=lambda k=k: collect_color(k)) 

    e.grid(row=i, column=j,padx=1,pady=1)

    buttons.append(e)

    j=j+1

    if(j%5==0):

        i=i+1

        j=0

my_w.mainloop()  # Keep the window open






沒有留言:

張貼留言

Node-Red --> MQTT --> Fuxa 開源碼網頁式圖控平台

Node-Red --> MQTT --> Fuxa      FUXA(一個開源的 Web HMI / SCADA 自動化監控軟體)的專案設定檔 。 這份設定檔完整定義了 HMI 監控畫面的 後端通訊(MQTT 連線、點位標籤) 與 前端網頁圖形介面(SVG 畫布...