私のプログラムはクリックロールダイスでなければなりません - 1,2,3,4,5,6ドットの6つのサイコロ画像を使用します。最初に1ドットで画像を表示し、画像をクリックしてからランダムに8回変えるべきです。最終的に、最後のランダムな画像は画面上にとどまるべきですが、うまくいかず、解決できません。どのようにそれを修正するための任意のアイデアですか? (私は3つのクラスが、このために必要ではないことを知っているが、私はこの問題を固溶しているだろうとき、私はそれに続けます) おかげPython - クリック時のイベントを伴うtkinterのオブジェクト指向の機能
import tkinter, random
class Main:
def __init__(self, area, dice):
dice.throw(area)
class Area:
def __init__(self):
self.canvas = tkinter.Canvas(width=1000, height=600)
self.canvas.pack()
self.dice1 = tkinter.PhotoImage(file='numberOne.png')
self.canvas.create_image(680,540,image=self.dice1)
self.canvas.bind('<Button-1>', Dice.throw)
class Dice:
def throw(event):
if event.x < 730 and event.x > 630 and event.y < 590 and event.y > 490:
dice1 = tkinter.PhotoImage(file='numberOne.png')
dice2 = tkinter.PhotoImage(file='numberTwo.png')
dice3 = tkinter.PhotoImage(file='numberThree.png')
dice4 = tkinter.PhotoImage(file='numberFour.png')
dice5 = tkinter.PhotoImage(file='numberFive.png')
dice6 = tkinter.PhotoImage(file='numberSix.png')
for i in range(8):
number = random.randrange(6) + 1
if number == 1:
area.canvas.create_image(680,540,image=dice1)
elif number == 2:
area.canvas.create_image(680,540,image=dice2)
elif number == 3:
area.canvas.create_image(680,540,image=dice3)
elif number == 4:
area.canvas.create_image(680,540,image=dice4)
elif number == 5:
area.canvas.create_image(680,540,image=dice5)
elif number == 6:
area.canvas.create_image(680,540,image=dice6)
area.canvas.after(100)
area.canvas.update()
pl = Area()
dice = Dice()
main = Main(pl, dice)
あなたのコードが動作しません正確にどのようにしてください。エラーがあれば貼り付けます。 – Nae
クリックイベントをどのようにランダムに表示するのか分かりません – Jozko
あなたはクラスインスタンスで 'bind( ''、dice.throw)'と一緒に使う必要があります。 'ダイス'を作る –
furas