0
Button을 OPButtun으로 서브 클래스 화하려고합니다. OPButton은 마우스가 움직일 때 도움말 메시지를 쓸 수있는 일반 버튼입니다. OPButton는 THRE 일반 버튼 생성자 받아 들일 것이 가능한 매개 변수 목록을 받아, 플러스 내 자신의 두해야합니다 메시지와 Stringvar 어디에서 작성Tkinter : 추가 인수가있는 Button 서브 클래스 화
이 내 코드 (아마도 실행 가능한)
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>", command=lambda e: string.set(message))
self.bind("<Leave>", command=lambda e: string.set(""))
if __name__ == '__main__':
root = Tk()
root.str= StringVar()
OPButton(root, root.str, "hovering the button", text="click here").pack()
ttk.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
편집 : 아래는 브라이언의 반응 후 수정 된 코드입니다. 완벽하게 작동합니다 (감사합니다).
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()
감사합니다. 나는 올바른 방향으로보고 있지 않았습니다. 나는 원래 게시물 끝에 디버깅 된 코드를 게시했습니다. 다시 한번 감사드립니다. 오류는 오타가 아닌 사소한 것이었지만 몇 시간 동안 붙어있었습니다. – quickbug
@quickbug 문제가 없습니다. :) –