質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

522閲覧

画像をランダムに出す方法

suzu

総合スコア3

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2022/02/22 14:27

編集2022/02/22 14:29

スロットゲームを作成したいのですが、ボタンを押す度にランダムになる方法が分かりません。random.randint(0,3)を使用するのは分かっているのですが...。

import tkinter as tk import random window=tk.Tk() window.geometry("640x480") window.title("Slot game") canvas=tk.Canvas(window,width=640,height=480,bg="white") canvas.place(x=0,y=0) #上の四角 canvas.create_rectangle(60,30,560,70,fill="#e6e6fa",outline="#000000") #四角 canvas.create_rectangle(30,90,210,260,fill="#ffffff",outline="#000000") canvas.create_rectangle(220,90,400,260,fill="#ffffff",outline="#000000") canvas.create_rectangle(410,90,590,260,fill="#ffffff",outline="#000000") #bottun canvas.create_oval(80,270,160,350,fill="#db7093",outline="#000000") canvas.create_oval(270,270,350,350,fill="#db7093",outline="#000000") canvas.create_oval(470,270,550,350,fill="#db7093",outline="#000000") #境界線 canvas.create_rectangle(7,360,630,380,fill="#bfc5ca",outline="#000000") #受け口 canvas.create_rectangle(180,400,460,460,fill="#696969",outline="#000000") #slot images cherry=tk.PhotoImage(file="cherry.png")#=0 seven=tk.PhotoImage(file="seven.png")#=1 bell1=tk.PhotoImage(file="bell1.png")#=2 heart=tk.PhotoImage(file="heart.png")#=3 canvas.create_image(33,95,image=cherry,anchor="nw") canvas.create_image(223,95,image=seven,anchor="nw") canvas.create_image(413,95,image=heart,anchor="nw") #ボタンの機能 leftbutton=tk.Button(window,text="push!",width=9) leftbutton.place(x=84,y=300) middlebutton=tk.Button(window,text="push!",width=9) middlebutton.place(x=274,y=300) rightbutton=tk.Button(window,text="push!",width=9) rightbutton.place(x=474,y=300)

画像です。
イメージ説明
イメージ説明
イメージ説明
イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

画像の入れ替えには canvas.itemconfig() を使います。

python

1#slot images 2N_SLOTS = 3 3image_files = ["cherry.png", "seven.png", "bell1.png", "heart.png"] 4images = [tk.PhotoImage(file=f) for f in image_files] 5n_images = len(images) 6image_pos = [(33 + 190*i, 95) for i in range(N_SLOTS)] 7images_on_canvas = [ 8 canvas.create_image(*xy, image=img, anchor="nw") 9 for img, xy in zip(images, image_pos)] 10 11#ボタンの機能 12def random_choise(nth): 13 canvas.itemconfig( 14 images_on_canvas[nth], image=images[random.randrange(0, n_images)]) 15 16leftbutton=tk.Button(window,text="push!",width=9, command=lambda nth=0: random_choise(nth)) 17leftbutton.place(x=84,y=300) 18 19middlebutton=tk.Button(window,text="push!",width=9, command=lambda nth=1: random_choise(nth)) 20middlebutton.place(x=274,y=300) 21 22rightbutton=tk.Button(window,text="push!",width=9, command=lambda nth=2: random_choise(nth)) 23rightbutton.place(x=474,y=300) 24 25window.mainloop()

投稿2022/02/22 17:59

編集2022/02/22 18:06
melian

総合スコア19714

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

suzu

2022/02/24 04:02

お忙しい中ありがとうございます。 このコードを実行したところ、どのボタンを押しても左側のみ絵が変わります。 それぞれボタンそれぞれ絵が変わるようにするにはどうすればいいですか?
melian

2022/02/24 04:09

こちらではそれぞれの画像が変化しています。
suzu

2022/02/24 14:01

出来ました!! ありがとうございます。感謝です!
guest

0

たぶんこんなかんじで

python

1images = [cherry,seven,bell1,heart] 2 3canvas.create_image(33,95,image=images[random.randint(0,3)],anchor="nw")

投稿2022/02/22 15:20

takasima20

総合スコア7458

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

suzu

2022/02/22 17:03

お忙しい中ありがとうございます。 再度入力してみたところ、実行(run module)するとランダムになるのですが、丸の中にあるボタンを押しても反応しません。ボタンを押してランダムにするにはどうすればいいのでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問