边框和背景颜色未在 mac [python -Tkinter] 中显示
Posted
技术标签:
【中文标题】边框和背景颜色未在 mac [python -Tkinter] 中显示【英文标题】:Border and Background color not showing in mac [python -Tkinter] 【发布时间】:2021-06-07 05:50:04 【问题描述】:我在 windows 中尝试过相同的代码,它工作正常,但在 mac GUI 按钮中不显示边框和背景颜色的效果。 例如。 btnc = Button(root,padx=28,pady=15,bd=5,fg='black',font=. ('arial',30,'bold'),text='C',bg='green').grid(row=1,column=3) 在上面的代码行 bg 和 bd 不起作用。
from tkinter import *
root = Tk()
root.title('Calculator')
#-------------------------------Screen-------------------------------
Display = Entry(root,font=('arial',30,'bold'),fg='white',bg='green',justify='right',bd=10)
Display.grid(columnspan=4)
#--------------------------------Row1--------------------------------
btn7 = Button(root,padx=30,pady=15,bd=5,fg='black',font=.
('arial',30,'bold'),text='7').grid(row=1,column=0)
btn8 = Button(root,padx=30,pady=15,bd=5,fg='black',font=.
('arial',30,'bold'),text='8').grid(row=1,column=1)
btn9 = Button(root,padx=30,pady=15,bd=5,fg='black',font=
('arial',30,'bold'),text='9').grid(row=1,column=2)
btnc = Button(root,padx=28,pady=15,bd=5,fg='black',font=.
('arial',30,'bold'),text='C',bg='green').grid(row=1,column=3)
#--------------------------------Row2--------------------------------
btn4 = Button(root,padx=30,pady=15,bd=5,fg='black',font=.
('arial',30,'bold'),text='4').grid(row=2,column=0)
btn5 = Button(root,padx=30,pady=15,bd=5,fg='black',font=.
('arial',30,'bold'),text='5').grid(row=2,column=1)
btn6 = Button(root,padx=30,pady=15,bd=5,fg='black',font=.
('arial',30,'bold'),text='6').grid(row=2,column=2)
btnplus = Button(root,padx=30,pady=15,bd=5,fg='black',font=.
('arial',30,'bold'),text='+',bg='orange').grid(row=2,column=3)
#--------------------------------Row3--------------------------------
btn1 = Button(root,padx=30,pady=15,bd=5,fg='black',font=.
('arial',30,'bold'),text='1').grid(row=3,column=0)
btn2 = Button(root,padx=30,pady=15,bd=5,fg='black',font=.
('arial',30,'bold'),text='2').grid(row=3,column=1)
btn3 = Button(root,padx=30,pady=15,bd=5,fg='black',font=
('arial',30,'bold'),text='3').grid(row=3,column=2)
btnminus = Button(root,padx=34,pady=15,bd=5,fg='black',font=('arial',30,'bold'),text='-
',bg='orange').grid(row=3,column=3)
#--------------------------------Row4--------------------------------
btn0 = Button(root,padx=30,pady=15,bd=5,fg='black',font=.
('arial',30,'bold'),text='0').grid(row=4,column=0)
btndot = Button(root,padx=34,pady=15,bd=5,fg='black',font=.
('arial',30,'bold'),text='.',bg='orange').grid(row=4,column=1)
btndivision = Button(root,padx=34,pady=15,bd=5,fg='black',font=.
('arial',30,'bold'),text='/',bg='orange').grid(row=4,column=2)
btnmultiply = Button(root,padx=33,pady=15,bd=5,fg='black',font=.
('arial',30,'bold'),text='*',bg='orange').grid(row=4,column=3)
#--------------------------------Row5--------------------------------
btnequals = Button(root,padx=74,pady=15,bd=5,fg='black',font=.
('arial',30,'bold'),text='=',bg='green').grid(row=5,column=0,columnspan=2)
btnopenbrackets = Button(root,padx=34,pady=15,bd=5,fg='black',font=.
('arial',30,'bold'),text='(',bg='orange').grid(row=5,column=2)
btnclosebrackets = Button(root,padx=33,pady=15,bd=5,fg='black',font=.
('arial',30,'bold'),text=')',bg='orange').grid(row=5,column=3)
root.mainloop()
【问题讨论】:
由于平台限制,您无法更改 OSX 上按钮的背景或边框。 【参考方案1】:试试这个:
from tkinter import *
root = Tk()
root.title('Calculator')
#-------------------------------Screen-------------------------------
root["bg"] = "blue"
Display = Entry(root,font=('arial',30,'bold'),fg='white',bg='green',justify='right',bd=10)
Display.grid(columnspan=4)
#--------------------------------Row1--------------------------------
btn7 = Button(root,padx=30,pady=15,bd=5,fg='black',font=('arial',30,'bold'),text='7').grid(row=1,column=0)
btn8 = Button(root,padx=30,pady=15,bd=5,fg='black',font=('arial',30,'bold'),text='8').grid(row=1,column=1)
btn9 = Button(root,padx=30,pady=15,bd=5,fg='black',font=
('arial',30,'bold'),text='9').grid(row=1,column=2)
btnc = Button(root,padx=28,pady=15,bd=5,fg='black',font=
('arial',30,'bold'),text='C',bg='green').grid(row=1,column=3)
#--------------------------------Row2--------------------------------
btn4 = Button(root,padx=30,pady=15,bd=5,fg='black',font=
('arial',30,'bold'),text='4').grid(row=2,column=0)
btn5 = Button(root,padx=30,pady=15,bd=5,fg='black',font=
('arial',30,'bold'),text='5').grid(row=2,column=1)
btn6 = Button(root,padx=30,pady=15,bd=5,fg='black',font=
('arial',30,'bold'),text='6').grid(row=2,column=2)
btnplus = Button(root,padx=30,pady=15,bd=5,fg='black',font=
('arial',30,'bold'),text='+',bg='orange').grid(row=2,column=3)
#--------------------------------Row3--------------------------------
btn1 = Button(root,padx=30,pady=15,bd=5,fg='black',font=
('arial',30,'bold'),text='1').grid(row=3,column=0)
btn2 = Button(root,padx=30,pady=15,bd=5,fg='black',font=
('arial',30,'bold'),text='2').grid(row=3,column=1)
btn3 = Button(root,padx=30,pady=15,bd=5,fg='black',font=
('arial',30,'bold'),text='3').grid(row=3,column=2)
btnminus = Button(root,padx=34,pady=15,bd=5,fg='black',font=('arial',30,'bold'),text='-',bg='orange').grid(row=3,column=3)
#--------------------------------Row4--------------------------------
btn0 = Button(root,padx=30,pady=15,bd=5,fg='black',font=
('arial',30,'bold'),text='0').grid(row=4,column=0)
btndot = Button(root,padx=34,pady=15,bd=5,fg='black',font=
('arial',30,'bold'),text='.',bg='orange').grid(row=4,column=1)
btndivision = Button(root,padx=34,pady=15,bd=5,fg='black',font=
('arial',30,'bold'),text='/',bg='orange').grid(row=4,column=2)
btnmultiply = Button(root,padx=33,pady=15,bd=5,fg='black',font=
('arial',30,'bold'),text='*',bg='orange').grid(row=4,column=3)
#--------------------------------Row5--------------------------------
btnequals = Button(root,padx=74,pady=15,bd=5,fg='black',font=
('arial',30,'bold'),text='=',bg='green').grid(row=5,column=0,columnspan=2)
btnopenbrackets = Button(root,padx=34,pady=15,bd=5,fg='black',font=
('arial',30,'bold'),text='(',bg='orange').grid(row=5,column=2)
btnclosebrackets = Button(root,padx=33,pady=15,bd=5,fg='black',font=
('arial',30,'bold'),text=')',bg='orange').grid(row=5,column=3)
root.mainloop()
【讨论】:
请解释一下好吗? Root["bg"] = "color" 将背景颜色更改为您设置的任何颜色 OSX 不允许您更改按钮的背景,这是 OP 所要求的。这个答案没有解决这个问题。以上是关于边框和背景颜色未在 mac [python -Tkinter] 中显示的主要内容,如果未能解决你的问题,请参考以下文章
在 IE 和 Mozilla Firefox 中,背景颜色和背景图像未在打印到 PDF 时显示