Python 以按钮为父级的类没有属性;tk";

Python 以按钮为父级的类没有属性;tk";,python,class,inheritance,button,tkinter,Python,Class,Inheritance,Button,Tkinter,我最近写了一些代码,其中我想创建另一个类,并将button作为父类(这是因为我想要一个button,它具有普通按钮不具有的各种方法),如下所示: from tkinter import * class some_class(Button): def __init__(self,parent): pass #i will put more here root = Tk() button = some_class(root) button.pack() mainlo

我最近写了一些代码,其中我想创建另一个类,并将button作为父类(这是因为我想要一个button,它具有普通按钮不具有的各种方法),如下所示:

from tkinter import *
class some_class(Button):
    def __init__(self,parent):
        pass
    #i will put more here 
root = Tk()
button = some_class(root)
button.pack()
mainloop()
但这样做会产生错误:

AttributeError: 'some_class' object has no attribute 'tk'
然后,如果我添加像
text=“hello”
这样的关键字,我会得到错误:

TypeError: __init__() got an unexpected keyword argument 'text'

我对类比较陌生,因此如果您能帮助我解释为什么会出现这种情况,我将不胜感激。

您必须使用
按钮。\uuuuu init\uuuu
在您的类中创建tkinter按钮

from tkinter import *


class MyButton(Button): # CamelCase class name

    def __init__(self, parent):
        Button.__init__(self, parent)
        # or
        #Button.__init__(self, parent, text="hello")


root = Tk()

button = MyButton(root)
button.pack()

root.mainloop()
如果需要在类中使用
text=“hello”
或其他参数 然后使用
*args、**kwargs

from tkinter import *


class MyButton(Button):

    def __init__(self, parent, *args, **kwargs):
        Button.__init__(self, parent, *args, **kwargs)


root = Tk()

button = MyButton(root, text='Hello World!')
button.pack()

root.mainloop()

非常感谢你!这真的很有帮助。