2012-04-18 2 views
2

나는 예를 들어 Class menu in Tkinter Gui 다음에 나오는 Tkinter 응용 프로그램을 시작하려고하지만, 예를 들면 다음과 같은 기능도 추가하려고합니다. 버튼 바, RadioButton 구성 바, 등과 같은 뭔가 :메뉴, 버튼 등. Tkinter GUI의 바

from Tkinter import * 

def clickTest(): 
    print "Click!" 

class App(Tk): 
    def __init__(self): 
     Tk.__init__(self) 
     menuBar = MenuBar(self) 
     buttonBar = ButtonBar(self) 

     self.config(menu=menuBar) 
     buttonBar.grid(row=0, column=0) ??? 

class MenuBar(Menu): 
    def __init__(self, parent): 
     Menu.__init__(self, parent) 

     fileMenu = Menu(self, tearoff=False) 
     self.add_cascade(label="File", menu=fileMenu) 
     fileMenu.add_command(label="Exit", command=clickTest) 

class ButtonBar(Frame): 
    def __init__(self, parent): 
     Frame.__init__(self, parent) 

     firstButton = Button(parent, text="1st Button", command=clickTest) 
     secondButton = Button(parent, text="2nd Button", command=clickTest) 

if __name__ == "__main__": 

    app = App() 
    app.mainloop() 

하지만이 같은 창에 표시하기 위해이 모든 것을 얻는 방법을 모르겠어요. 물론,있는 그대로의 코드는 작동하지 않습니다. 모든 제안을 부탁드립니다. 감사!

답변

0

pack()으로 했습니까? 나는 그것이 또한 grid()으로 행해질 수 있다고 확신하지만 나는 그것에 익숙하지 않다. 또 다른 한가지는

from Tkinter import * 

def clickTest(): 
    print "Click!" 

class App(Tk): 
    def __init__(self): 
     Tk.__init__(self) 
     menuBar = MenuBar(self) 
     buttonBar = ButtonBar(self) 

     self.config(menu=menuBar) 
     buttonBar.pack() 

class MenuBar(Menu): 
    def __init__(self, parent): 
     Menu.__init__(self, parent) 

     fileMenu = Menu(self, tearoff=False) 
     self.add_cascade(label="File", menu=fileMenu) 
     fileMenu.add_command(label="Exit", command=clickTest) 

class ButtonBar(Frame): 
    def __init__(self, parent): 
     Frame.__init__(self, parent) 

     firstButton = Button(self, text="1st Button", command=clickTest).pack() 
     secondButton = Button(self, text="2nd Button", command=clickTest).pack() 

if __name__ == "__main__": 

    app = App() 
    app.mainloop() 

,이 라인에서 같은 프레임과 같은 버튼의 부모를 설정해야합니다

firstButton = Button(self, text="1st Button", command=clickTest).pack() 

여기에 내가 selfparent을 변경했습니다. self은 프레임 자체이며 전체 최상위 창은 아닙니다. 그리고 pack() 기능을 사용하면이 버튼이 부모 인 버튼에 포장됩니다.이 경우에는 프레임입니다.

그러면 buttonBar.pack()을 사용하여 buttonBar를 최상위 창에 압축했습니다. 여기 및 프레임에서 눈금을 사용할 수도 있습니다.

+0

차가움. 감사. 네, 방금 그리드 (grid)로 알아 냈습니다. 버튼의 부모에 대한 마지막 제안을 주셔서 감사합니다. – Ryan