2011-02-02 8 views
0

Menu라는 클래스를 만들려고합니다.이 클래스는 주어진 위젯에 대해 오른쪽 클릭 메뉴를 만듭니다. 이 경우 자기. 라벨.Python 클래스 이해하기 사용자 정의 위젯을 만들기 위해

Traceback (most recent call last): 
    File "<string>", line 245, in run_nodebug 
    File "<module1>", line 57, in <module> 
    File "<module1>", line 55, in run 
    File "<module1>", line 52, in __init__ 
    File "<module1>", line 12, in __init__ 
    File "C:\Python26\Lib\Tkinter.py", line 2595, in __init__ 
    Widget.__init__(self, master, 'menu', cnf, kw) 
    File "C:\Python26\Lib\Tkinter.py", line 1923, in __init__ 
    BaseWidget._setup(self, master, cnf) 
    File "C:\Python26\Lib\Tkinter.py", line 1901, in _setup 
    self.tk = master.tk 
AttributeError: B3Menu instance has no attribute 'tk' 

내 프로그램 : : 내 프로그램을 실행할 때

그러나, 나는 다음과 같은 오류가 발생합니다

import Tkinter 

class B3Menu: 
    def __init__ (self, wid): 

     self.wid = wid 

     self.MeVar = Tkinter.StringVar() 

     self.Me = Tkinter.Menu(self, tearoff=0, 
            activebackground='grey15', 
            activeforeground='grey95') 

     self.Me.add_radiobutton(label='Cut', variable=self.MeVar, 
            command=self.menu_beh, 
            accelerator='Ctrl-x') 

     self.Me.add_radiobutton(label='Copy', variable=self.MeVar, 
            command=self.menu_beh, 
            accelerator='Ctrl-c') 

     self.Me.add_separator() 

     self.Me.add_radiobutton(label='Paste', variable=self.MeVar, 
            command=self.menu_beh, 
            accelerator='Ctrl-v') 

     self.wid.bind("<ButtonRelease-3>", self.menu_pos) 

    def menu_pos (self, event=None): 
     self.Me.post(event.x_root, event.y_root) 

    def menu_beh (self): 
     ''' Handles the behavior of right click menu ''' 

     if self.MeVar.get() =='Cut': 
      self.wid.event_generate("<<Cut>>") 

     if self.MeVar.get() =='Copy': 
      self.wid.event_generate("<<Copy>>") 

     if self.MeVar.get() =='Paste': 
      self.wid.event_generate("<<Paste>>") 

class Suite(Tkinter.Tk): 
    def __init__(self): 
     Tkinter.Tk.__init__(self) 

     self.Label = Tkinter.Label(self, text='hello') 
     self.Label.pack() 


     B3Menu(self.Label) 

def run(): 
    Suite().mainloop() 
if __name__ == '__main__': 
    run() 

이것은 파이썬 클래스 시스템을 사용하여 위젯을 만드는에서 내 첫 번째 시도이다. 그래서 나는 많은 것을 잘못하고 있다고 확신합니다. 어떤 도움이라도 대단히 감사 할 것입니다.

답변

2

어쩌면 일부 기본 위젯 클래스에서 상속해야합니까?

관련 문제