Python Tkinter:使用附加参数对按钮进行子类化

Python Tkinter:使用附加参数对按钮进行子类化,python,tkinter,constructor,subclass,Python,Tkinter,Constructor,Subclass,我想将Button子类化为OpButton。OPButton是一个常规按钮,具有在鼠标悬停时编写帮助消息的功能。OPButton必须接受常规Button构造函数可以接受的任何可能的参数列表,加上我自己的两个参数:一条消息和Stringvar,在其中写入它 这是我的代码(应该可以运行) 从tkinter导入* 从tkinter导入ttk 类操作按钮(按钮): """ """ 定义初始化(self,parent,string,message,*args,**kwargs): ttk.Button.\

我想将Button子类化为OpButton。OPButton是一个常规按钮,具有在鼠标悬停时编写帮助消息的功能。OPButton必须接受常规Button构造函数可以接受的任何可能的参数列表,加上我自己的两个参数:一条消息和Stringvar,在其中写入它

这是我的代码(应该可以运行)

从tkinter导入*
从tkinter导入ttk
类操作按钮(按钮):
""" """
定义初始化(self,parent,string,message,*args,**kwargs):
ttk.Button.\uuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
self.bind(“,command=lambda e:string.set(message))
self.bind(“”,command=lambda e:string.set(“”)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
root=Tk()
root.str=StringVar()
OPButton(root,root.str,“悬停按钮”,text=“单击此处”).pack()
Label(root,textvariable=root.str).pack()
root.mainloop()
以及错误消息:

Traceback (most recent call last):
  File "C:\Users\planchoo\oPButton.py", line 19, in <module>
    OPButton(root, "Hello World", "Bouton", text="Hello").pack()
TypeError: __init__() takes from 1 to 3 positional arguments but 4 were given
回溯(最近一次呼叫最后一次):
文件“C:\Users\planchoo\oPButton.py”,第19行,在
OPButton(root,“Hello World”,“Bouton”,text=“Hello”).pack()
TypeError:\uuuu init\uuuuu()接受1到3个位置参数,但给出了4个
编辑:下面是Bryan回复后更正的代码。工作完美(谢谢)

从tkinter导入*
从tkinter导入ttk
类操作按钮(按钮):
""" """
定义初始化(self,parent,string,message,*args,**kwargs):
ttk.Button.\uuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
self.bind(“,lambda e:string.set(消息))
self.bind(“”),lambda e:string.set(“”)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
root=Tk()
root.chaine=StringVar()
OPButton(root,root.chaine,“Bouton”,text=“Hello”).pack()
Label(root,textvariable=root.chaine).pack()
root.mainloop()

我很确定您定义的
\uuuuu init\uuuuu()
函数拼写为
\uuuuuuu init\uuuuu()
,谢谢。我看的方向不对。我在原始帖子的末尾发布了调试过的代码。再次感谢。虽然这个错误和打字错误一样微不足道,但我还是被困了好几个小时。@quickbug没问题。:)
Traceback (most recent call last):
  File "C:\Users\planchoo\oPButton.py", line 19, in <module>
    OPButton(root, "Hello World", "Bouton", text="Hello").pack()
TypeError: __init__() takes from 1 to 3 positional arguments but 4 were given
from tkinter import *
from tkinter import ttk

class OPButton(Button):
    """ """
    def __init__(self, parent, string, message, *args, **kwargs):
        ttk.Button.__init__(self, parent, *args, **kwargs)
        self.bind("<Enter>", lambda e: string.set(message))
        self.bind("<Leave>", lambda e: string.set(""))

if __name__ == '__main__':
    root = Tk()
    root.chaine = StringVar()
    OPButton(root, root.chaine, "Bouton", text="Hello").pack()
    ttk.Label(root, textvariable=root.chaine).pack()
    root.mainloop()