2012-04-18 7 views
2

Class menu in Tkinter Guiの後ろに続くTkinterアプリケーションを開始しようとしていますが、この機能を追加することもできます。ボタン・バー、ラジオボタンバーなどのような何か:メニュー、ボタンなど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

Cool。ありがとう。ええ、私もgrid()でそれを理解しました。ボタンの親のための最後の提案をありがとう - それは私のためにそれを修正した。 – Ryan