前提
pythonのtkinterで簡易的なタイピングゲームを作っています。
実行して遊び方のボタンをクリックしたら別のラベルを表示し、タイトルに戻るというボタンを押したら初期画面に戻るようにしたいです。そのため、タイトルに戻るボタンを押したら、commandでmain()という初期画面を設定している画面に変更する関数を呼び出そうとしているのですが、呼び出せません。
実現したいこと
タイトルに戻るというボタンを押したら初期画面に戻り、それを何回でも続けられるようにしたいです。
発生している問題・エラーメッセージ
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\nonch\AppData\Local\Programs\Thonny\lib\tkinter_init_.py", line 1705, in call
return self.func(*args)
File "C:\Users\nonch\Documents\pythonフォルダー\keses.py", line 74, in main
title_label.pack(ipady=60)
File "C:\Users\nonch\AppData\Local\Programs\Thonny\lib\tkinter_init_.py", line 2143, in pack_configure
+ self._options(cnf, kw))
_tkinter.TclError: bad window path name ".!label7"
該当のソースコード
python
import tkinter as tk
root = tk.Tk()
root.geometry("1200x600")
root.title("タイピングゲーム")
words = ["ishikawa", "kanazawa", "hokuriku","nagano"]
index = 0
number_of_words = 0
#関数内で使うグローバル変数
word_label = tk.Label(text=words[index], foreground="#66CCFF", font=("",50))
ans_label = tk.Label(text="", font=("",60)) #キー入力の受け取り
count_label = tk.Label(text=4, font=("",100)) #スタートカウントダウンの消去
time_left_label = tk.Label(text="残り時間: 秒", font=("",20))
timer_label = tk.Label(text=31, font=("",20))
number_of_words_label = tk.Label(text=f"タイプワード数:{number_of_words}", font=("",20))
num = 4
timer_num = 31
#####タイプ画面#####
def create_widgets():
word_label.pack(ipady=100)
ans_label.pack()
time_left_label.place(x=0, y=0)
timer_label.place(x=140, y=0)
number_of_words_label.place(x=0, y=50)
timer()
def timer():
global timer_num
if timer_num == timer_label["text"]:
timer_num -= 1
timer_label.configure(text=timer_num)
if timer_num > 0:
root.after(1000, timer)
#####カウントダウンとウィジェットの消去#####
def countdown():
start_button.destroy()
title_label.destroy()
rule_button.destroy()
count_label.pack(expand=True)
global num
if num == count_label["text"]:
num -= 1
count_label.configure(text=num)
if num > 0:
root.after(1000, countdown)
elif num == 0: count_label.destroy() create_widgets()
#####遊び方#####
def rule():
rule_label.pack(ipady=60)
title_label.destroy()
rule_button.destroy()
start_button.destroy()
return_button.pack()
title_label = tk.Label(text="タイピングゲーム", font=("",50)) #グローバル変数として定義しないと関数内で消去できない
rule_button = tk.Button(text="遊び方", font=("",30), command=rule)
start_button = tk.Button(text="スタート", font=("",30), command=countdown)
def main():
#####初期画面#####
#title_label = tk.Label(text="タイピングゲーム", font=("",50)) #グローバル変数として定義しないと関数内で消去できない
title_label.pack(ipady=60)
#rule_button = tk.Button(text="遊び方", font=("",30), command=rule)
rule_button.place(x=750, y=300)
#start_button = tk.Button(text="スタート", font=("",30), command=countdown)
start_button.place(x=250, y=300)
main()
rule_label = tk.Label(text="~遊び方~", font=("",50))
return_button = tk.Button(text="タイトルに戻る", font=("",30), command=main)
#####キー入力#####
def type_event(event):
global index
global number_of_words
ans_label["text"] += event.keysym
if ans_label["text"] == word_label["text"]:
ans_label.configure(text="")
index += 1
word_label.configure(text=words[index])
number_of_words += 1
number_of_words_label.configure(text=f"タイプワード数:{number_of_words}")
elif event.keysym == "BackSpace": text = ans_label["text"] ans_label["text"] = text[:-10]
root.bind("<KeyPress>", type_event) #関数とキー入力の紐づけ
root.mainloop()

回答1件
あなたの回答
tips
プレビュー