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](/images/python/MacOS%E4%B8%8BPython%E4%B8%ADTkinter%E6%8C%89%E9%92%AE%E6%97%A0%E6%B3%95%E4%BF%AE%E6%94%B9%E8%83%8C%E6%99%AF%E7%9A%84%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/result1.png)
遗憾的是,通过bg
属性设置按钮背景色发现,不生效。
按钮的背景色是MacOS系统默认颜色风格。需引用tkmacosx库。
2.解决方案
安装tkmacosx
并使用新Button
2.1. 按钮引用库更换
由from tkinter import Button
更换为from tkmacosx import Button
2.2.按钮宽高设定更换
tkmacosx下需要重新设置了button宽度与高度
1
2
|
WIDGETWIDTH = 500
WIDGETHEIGHT = 50
|
最终效果
![image](/images/python/MacOS%E4%B8%8BPython%E4%B8%ADTkinter%E6%8C%89%E9%92%AE%E6%97%A0%E6%B3%95%E4%BF%AE%E6%94%B9%E8%83%8C%E6%99%AF%E7%9A%84%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/result2.png)
文章作者
梵梵爸
上次更新
2024-06-07
许可协议
原创文章,如需转载请注明文章作者和出处。谢谢