[英]How do I change the background colour and foreground colour using a button in Tkinter?
[英]How to change the foreground or background colour of a Tkinter Button on Mac OS X?
我一直在閱讀 Python 中的 Tkinter 章節,遇到了一個按鈕的前景色和背景色不會改變的問題。 我正在使用 Python 2.6.1 的 Mac OS X 10.6 系統。 label 的顏色會改變,但按鈕的顏色不會改變。 例如:
from Tkinter import *
Label(None, text='label', fg='green', bg='black').pack()
Button(None, text='button', fg='green', bg='black').pack()
mainloop()
在我的 Mac 系統上,label 的顏色會改變,但按鈕的顏色不會。 在帶有 Python 2.6.1 的 Windows 系統上,label 和按鈕的顏色都發生了變化。
有誰知道出了什么問題?
我檢查了 Interface Builder,似乎沒有選項可以更改該工具中按鈕的前景色或背景色。 可以編輯 label 的前景色和背景色。
Mac OS X 渲染系統(Quartz?)可能只是不支持(輕松)更改按鈕的 fg 和 bg。
我認為答案是 mac 上的按鈕根本不支持更改背景和前景色。 正如您所見,這並不是 Tk 獨有的。
對於像我一樣遇到這個問題的其他人,解決方案是使用ttk模塊,該模塊在 OS X 10.7 上默認可用。 不幸的是,設置背景顏色仍然無法開箱即用,但文本顏色可以。
它需要對代碼稍作改動:
原來的:
from Tkinter import *
Label(None, text='label', fg='green', bg='black').pack()
Button(None, text='button', fg='green', bg='black').pack()
mainloop()
使用 ttk:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
# background="..." doesn't work...
ttk.Style().configure('green/black.TLabel', foreground='green', background='black')
ttk.Style().configure('green/black.TButton', foreground='green', background='black')
label = ttk.Label(root, text='I am a ttk.Label with text!', style='green/black.TLabel')
label.pack()
button = ttk.Button(root, text='Click Me!', style='green/black.TButton')
button.pack()
root.mainloop()
您可以使用 PyPI 中的tkmacosx庫來實現。
安裝:
對於 Python 2,請使用pip install tkmacosx
。
對於 Python 3,使用pip3 install tkmacosx
。
這是您使用tkmacosx
:
from tkinter import *
from tkmacosx import Button
root = Tk()
B1 = Button(root, text='Mac OSX', bg='black',fg='green', borderless=1)
B1.pack()
root.mainloop()
它在 Mac OS X 上運行良好。
很煩人,多年后這仍然是一個問題。
無論如何,正如其他人所提到的,highlightbackground(邊框顏色)可以用來代替 Mac 上的背景。 如果您將邊框的大小增加到很大(按鈕的大小或更大),您將獲得漂亮的純色背景色。 這將使您的按鈕具有標簽的外觀。
如果您使用 place,這有效,但如果您使用類似 grid 的東西,則無效。 不幸的是,對於網格,增加邊框大小會自動增加按鈕大小。
然而,如果你必須使用網格,你總是可以破解它......創建你的無色網格按鈕。 接下來使用 place 在它上面設置一個背景顏色按鈕。 這將是帶有“命令”的按鈕或您將事件綁定到的按鈕。
如果您希望您的代碼獨立於操作系統,您可以添加一個“if OS ==“Mac””語句,或者甚至添加一個自定義函數來修改按鈕,如果它在 Mac 上,但在 Windows 或 Linux 上則保留它。 這是前者:
from tkinter import *
import platform
if platform.system() == "Darwin": ### if its a Mac
B = Button(text="Refersh All Windows", highlightbackground="Yellow", fg="Black", highlightthickness=30)
else: ### if its Windows or Linux
B = Button(text="Refresh All Windows", bg="Yellow", fg="Black")
B.place(x=5, y=10, width=140, height=30)
mainloop()
這對我有用:
self.gnuplot_bt = Button(
self.run_but_container, text="Plot with Gnuplot", font="Helvetica", command=self.gnuplot,
highlightbackground ="#8EF0F7", pady=2, relief=FLAT
)
我一直在尋找為什么這也不起作用。 我找到了一種嘗試修復它的快速方法是擁有一個標簽,然后將點擊與標簽綁定。 然后讓標簽在短時間內改變顏色以模擬點擊。 這是一個例子。
def buttonPress(*args):
searchB.config(state = "active")
searchB.update()
time.sleep(0.2)
searchB.config(state = "normal")
## Whatever command you want
searchB = Label(main, text = "Search", bg = "#fecc14", fg = "Black", activebackground = "Red", highlightbackground="Black")
searchB.bind("<Button-1>", startSearch)
searchB.pack()
確認以下代碼可以在 Mac OS X 上更改 tkinter Button 的背景。
self.btn_open = tk.Button(self.toolbar,
text = "Open",
command=self.open,
highlightbackground = "gray")
但它不能改變 ttk.Button 的背景。
不確定是否有人仍在查看此線程,但我通過創建自己的按鈕 class 創建了一個簡單的解決方案來解決這個問題。 它在GitHub上可用。
import tkinter as tk
class Button():
button_frame = None
root = None
width=100
height=20
text=""
bg="white"
fg="black"
font="f 12"
bordercolor = "black"
bordersize = 3
label = None
command = None
def __init__(self,root,width=100,height=20,text="",bg="white",fg="black",font="f 12",command=None,bordercolor="black",bordersize=0):
self.root = root
self.width=width
self.height=height
self.text=text
self.bg=bg
self.fg=fg
self.font=font
self.command = command
self.bordercolor = bordercolor
self.bordersize = bordersize
self.button_frame = tk.Frame(root,width=width,height=height,bg=bg)
self.label = tk.Label(self.button_frame,text=self.text,bg=self.bg,width=self.width,height=self.height,fg=self.fg,font=self.font,highlightbackground=self.bordercolor,highlightthickness=self.bordersize)
self.label.place(anchor="center",relx=0.5,rely=0.5,relheight=1,relwidth=1)
self.label.bind("<Button-1>",self.call_command)
def call_command(self,event):
if (self.command != None):
self.command()
def place(self,anchor="nw",relx=0,rely=0):
self.button_frame.place(anchor=anchor,relx=relx,rely=rely)
def configure(self,width=width,height=height,text=text,bg=bg,fg=fg,font=font,command=command,bordercolor=bordercolor,bordersize=bordersize):
self.button_frame.configure(width=width,height=height,bg=bg)
self.label.configure(text=text,bg=bg,width=width,height=height,fg=fg,font=font,highlightbackground=bordercolor,highlightthickness=bordersize)
self.command =
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.