2014-06-05 29 views
0

所以,我在tkinter框架中有很多不同的按钮和标签,而且我都希望它们具有相似的属性。可以说我希望所有人都拥有红色的前景色,并且具有透明背景(我可以这么做吗?这个透明背景只适用于按钮。)Python tkinter为按钮和标签制作'classes'

我可以为按钮设置'class'(我认为这是在ttk中,但它会更好,如果它不是)类似于CSS,这将使我的所有按钮和标签都有红色文字?

回答

3

您可以扩展Button类,并根据需要定义其属性。例如:

from tkinter import * 


class MyButton(Button): 

    def __init__(self, *args, **kwargs): 
     Button.__init__(self, *args, **kwargs) 
     self['bg'] = 'red' 



root = Tk() 
root.geometry('200x200') 

my_button = MyButton(root, text='red button') 
my_button.pack() 

root.mainloop() 

enter image description here

+0

哇!现在我明白了,我觉得它很明显。谢谢! – Kevin

+0

但是我如何在课堂上做到这一点?我会做self.MyButton()? – Kevin

+0

@凯文,但在课堂上做什么?打包按钮? – Marcin