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