1

私は、Guess Who スタイルのゲームを作成しようとしているエントリー レベルの Python コーダーです。大学では、画像をインポートして画面上の固定位置にバインドする方法をまだ学んでいません (ゲーム ボードに似ています)。特定の画像をクリックして、その特定の文字 (画像) が選択されている場合に onClickEvent を発生させる方法はありますか? 私のコーディング能力の大部分は python にありますが、これがこのようなプロジェクトを実行するのに最適な言語であるかどうかは懐疑的です.

4

2 に答える 2

10

すべての GUI にButtonは、クリック可能で (ほとんどの場合) 画像を表示できるウィジェットがあります。

しかし、ほとんどの GUI では、すべてのオブジェクトにクリック イベントを割り当てることができます。LabelImage

すなわち。トキンター

import tkinter as tk
from PIL import Image, ImageTk

# --- functions ---

def on_click(event=None):
    # `command=` calls function without argument
    # `bind` calls function with one argument
    print("image clicked")

# --- main ---
    
# init    
root = tk.Tk()

# load image
image = Image.open("image.png")
photo = ImageTk.PhotoImage(image)

# label with image
l = tk.Label(root, image=photo)
l.pack()

# bind click event to image
l.bind('<Button-1>', on_click)

# button with image binded to the same function 
b = tk.Button(root, image=photo, command=on_click)
b.pack()

# button with text closing window
b = tk.Button(root, text="Close", command=root.destroy)
b.pack()
    
# "start the engine"
root.mainloop()

のようなグラフィックモジュールPyGameも画像を表示でき、クリックイベントがありますが、画像のある領域をクリックしたかどうかを手動で確認する必要がある場合があります (mainloop手動で作成する必要があります) 。

于 2016-11-18T00:36:20.723 に答える