[英]How to get a value for “foreground” in tkinter/ttk python?
我有一個標簽對象,我將要更改其背景顏色,並且我想有一種方法可以檢查給定時間的背景顏色。 例如:
root=Tk()
label=ttk.Label(root, text="Hello there", background="#000000", foreground="#ffffff")
label.pack()
root.mainloop()
oldfg=label.cget("foreground")
然后調用oldfg
給出了非常模糊的<color object at 0x04078168>
。 有沒有一種方法可以獲取顏色的十六進制或rgb表示,或者可以某種方式使用它?
編輯:在標題中我說前景,在代碼中我說背景,同樣的事情適用於兩者。
我認為您已經回答了自己的問題以證明這一點:
import Tkinter as tk
def swapsies():
oldfg = label.cget("foreground")
oldbg = label.cget("background")
label.config(background=oldfg, foreground=oldbg)
print "Foreground: {0} Background: {1}".format(oldfg, oldbg)
root = tk.Tk()
label = tk.Label(root, text="Hello there", background="#000000", foreground="#ffffff")
label.pack(side=tk.LEFT)
mega_button = tk.Button(root, text="GO!", command=swapsies)
mega_button.pack(side=tk.LEFT)
root.mainloop()
關閉輸出,然后單擊按鈕交換顏色:
"Foreground: #ffffff Background: #000000"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.