2016-10-31 4 views
1

Pythonでフレームに関する情報を取得するために、単一の関数を使用します - イベント、フレームとTkinterの

ここで私はTkinterのアプリケーションを作成し、実行し、私のGameApplicationクラスを持っています。私はすべてのフレームから同じ関数を呼び出すことができ、その関数を使用して、どのフレームがクリックされたかを調べることができるようにしたいと思います。私はちょうど今Pythonにダイビングしているので、これは単純な場合私は言い訳です。Pythonの - どのように私はTkinterの

ご協力ありがとうございます。

# Import needed classes. 
from tkinter import * 


# Create the GameApplication class to run the GUI 
class GameApplication(Frame): 
    def __init__(self, master=None): 
     super().__init__(master) 
     self.grid() 
     self.grid_rowconfigure(0, weight=1) 
     self.grid_columnconfigure(0, weight=1) 

     self.position_1 = Frame(width=100, height=100, bg="#CC0000") 
     # This is the Frame I want to get information about. 
     self.position_1.bind("<Button-1>", self.callback) 
     self.position_1.grid(column=0, row=0) 

     self.position_2 = Frame(width=100, height=100, bg="#00CC00") 
     # And this one too. 
     self.position_2.bind("<Button-1>", self.callback) 
     self.position_2.grid(column=1, row=0) 

    def callback(self, event): 
     print("?") 

if __name__ == "__main__": 
    root = Tk() 
    app = GameApplication(master=root) 
    root.mainloop() 
+0

各フレームに意味のある名前を付けることができます: 'Frame(name = '...'、...)'そして 'callback(...)'関数のフレーム名を 'str(イベント。ウィジェット) '。 – acw1668

答えて

1

これを見てください。それは私がPythonで書いた小さなプロジェクトです。私は各呼び出しの状態をチェックする機能を持っています。私は各関数が実行された後にその関数を呼び出します。

class Messengers: 
    def __init__(self): 
     self.box = tkMessageBox 

    def successer(self): 
     self.box.showinfo("ImageR Success", "Done YO! Go run a test :)") 

    def failure(self): 
     self.box.showerror('ImageR Failure', 'Yo you broke me!') 

コードはここにhttps://github.com/jaytarang92/imagerです。 subprocess.check_ouputを使用して、呼び出しが正しく行われたことを確認します。

関連する問題