我正在尝试在Tkinter的单个按钮上放置多种颜色.这是我想出的:
from Tkinter import *
master = Tk()
buttons = {}
def press(clicked):
print clicked
return
for i in range(1,7):
button_name = 'button '+str(i)
buttons[i] = Button(master, text=button_name, command=lambda method=button_name:press(method))
buttons[i].grid(row=0, column=i-1, padx=15)
buttons[i].update()
tot_x = buttons[i].winfo_width()
tot_y = buttons[i].winfo_height()
redframe = Frame(buttons[i], width=tot_x/6, background='red')
blueframe = Frame(buttons[i], width=tot_x/6*5, background='blue')
redframe.place(x=0, y=0, relheight=1)
blueframe.place(relx=1./6, y=0, relheight=1)
master.mainloop()
问题在于它使按钮几乎不可单击,因为框架以某种方式干扰了按钮的“可单击性”.当光标放在其位置时,彩色框也会突然消失.有没有办法使这些框架停留在按钮上并使它们不停止单击?
解决方法:
可以说,最简单的方法可能是使用所需的颜色创建图像,然后将按钮中的图像与文本一起使用.
标签:python-2-7,tkinter,python
来源: https://codeday.me/bug/20191025/1925795.html