Pythonでオンライン講義どおりジャンケンゲームを作りましたが、エラーとなります。 解決方法を教示いただきたいです。(2)

実現したいこと

ジャンケンゲームを実行する
※classを使いコーディング実施
※initの表示の際の入力ミスを修正したが、別のエラーが出てしまった。

発生している問題・分からないこと

プログラムを実行しようとしたところエラー箇所が2つ指摘されました。
Googleで検索して解決策を探しましたがよくわかりませんでした。

エラーメッセージ

error

1PS C:\Users\User\Udemy> cd janken 2PS C:\Users\User\Udemy\janken> python janken_gui_class.py 3Traceback (most recent call last): 4 File "janken_gui_class.py", line 78, in <module> 5 app = Application(master=root) 6 File "janken_gui_class.py", line 67, in __init__ 7 self.view.gu_button['command'] = lambda: self.judge(0) 8AttributeError: 'View' object has no attribute 'gu_button' 9PS C:\Users\User\Udemy\janken>

該当のソースコード

import random import tkinter as tk from PIL import Image, ImageTk GU, CHOKI, PA = 'グー', 'チョキ', 'パー' hands = [GU, CHOKI, PA] WIN, DRAW, LOSE = "勝ち", "あいこ", "負け" rules = { (0, 0): DRAW, (0, 1): WIN, (1, 0): LOSE, (1, 0): LOSE, (0, 0): DRAW, (0, 1): WIN, (2, 0): WIN, (2, 1): LOSE, (0, 0): DRAW } class View: def __init__(self): self.gu_image = Image.open('img/gu.png').convert('RGB').resize((100, 100)) self.gu_image = ImageTk.PhotoImage(self.gu_image) self.choki_image = Image.open('img/choki.png').convert('RGB').resize((100, 100)) self.choki_image = ImageTk.PhotoImage(self.choki_image) self.pa_image = Image.open('img/pa.png').convert('RGB').resize((100, 100)) self.pa_image = ImageTk.PhotoImage(self.pa_image) self.gu_label = tk.Label(root, image=self.gu_image) self.gu_label.place(x=20, y=200) self.choki_label = tk.Label(root, image=self.choki_image) self.choki_label.place(x=160, y=200) self.pa_label = tk.Label(root, image=self.pa_image) self.pa_label.place(x=300, y=200) self.gu_btn = tk.Button(root, text='グー') self.gu_btn.place(x=50, y=320) self.choki_btn = tk.Button(root, text='チョキ') self.choki_btn.place(x=190, y=320) self.pa_btn = tk.Button(root, text='パー') self.pa_btn.place(x=340, y=320) self.enemy_label = tk.Label(root, image=self.gu_image) self.enemy_label.place(x=160, y=20) self.text_label = tk.Label(root, text='最初はグー!じゃんけん!') self.text_label.place(x=140, y=140) self.retry_btn = tk.Button(root, text='リトライ') def reset(self): pass class Application(tk.Frame): def __init__(self, master=None): super().__init__(master) master.geometry('420x420') master.title('ジャンケンゲーム') self.view = View() self.view.gu_button['command'] = lambda: self.judge(0) self.view.choki_button['command'] = lambda: self.judge(1) self.view.pa_button['command'] = lambda: self.judge(2) def judge(self, my_hand): pass def retry(self): self.view.reset() root = tk.Tk() app = Application(master=root) app.mainloop()

試したこと・調べたこと

上記の詳細・結果

解決策を見つけることができませんでした。

補足

バージョン Python 3.8.2

コメントを投稿

0 コメント