1.MacOS下Python中Tkinter按钮无法修改背景的解决方案

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22


    WIDGETWIDTH = 30
    WIDGETHEIGHT = 2
    BUTTON_FONT_SIZE = 20 
    BUTTON_BKCOLOR = "#5B70CF"
    BUTTON_BKCOLOR_SELECTED = "#5B70FF"
    BUTTON_FONT_COLOR = "white"

     for item in self.item_list:
         
            button = Button(self.root, text=item,
                            width=App.WIDGETWIDTH,
                            height=App.WIDGETHEIGHT,
                            font=("Arial", App.BUTTON_FONT_SIZE),
                            bg=App.BUTTON_BKCOLOR,
                            fg=App.BUTTON_FONT_COLOR,
                            activebackground=App.BUTTON_BKCOLOR_SELECTED
                            )
            button["command"] = lambda item=item: self.on_button_click(item)
            button.pack(pady=20)
            self.button_list.append(button)

image

遗憾的是,通过bg属性设置按钮背景色发现,不生效。

按钮的背景色是MacOS系统默认颜色风格。需引用tkmacosx库。

2.解决方案

安装tkmacosx并使用新Button

1
pip3 install tkmacosx

2.1. 按钮引用库更换

from tkinter import Button更换为from tkmacosx import Button

2.2.按钮宽高设定更换

tkmacosx下需要重新设置了button宽度与高度

1
2
    WIDGETWIDTH = 500
    WIDGETHEIGHT = 50

最终效果

image