问题描述
这是一个简单的记忆游戏,声音说出一个数字,您必须单击具有相同数字的按钮。我想将其与比较列表一起使用。如果您猜对了一个,则该代码将添加另一个,另一个,依此类推。我的问题是,我有按钮,然后单击功能按钮,可以用print定义值。我有应该链接的代码,但是无法将它们排列在一起。请帮助我:)
import pyttsx3
import time
import random
import tkinter as tk
from tkinter import *
root = Tk()
root.title("A little memory game")
sound_or_color = Intvar() # for radiobutton 1,sound,2 color
quiz_list = [] # this will be the random number
your_list = [] # this will get the input form the buttons
guess = StringVar() # for the label to add guesses
best_score = 2
best_score_final = Intvar()
best_score_final.set(best_score)
actual_score_actual = Intvar()
def button_click(number):
your_list.append(number)
print(your_list)
def game():
# if sound_or_color == 1: # this will work with sound
actual_score = 0
global best_score
while True:
# this will generate the task,each round
quiz_number = random.randint(1,9)
quiz_list.append(quiz_number)
speaker = pyttsx3.init()
speaker.say(quiz_list)
speaker.runAndWait()
print(quiz_list)
guess.set("click your guess(es)")
**# take the user's guess:
for i in range(0,len(quiz_list)):
# your_list.append(int(input())) <------the problematic part
button_click()**
# was the guess right?
if quiz_list == your_list:
actual_score += 1
actual_score_actual.set(actual_score)
your_list.clear()
else:
end_text = "Coungratulation,your score is",actual_score
your_list.clear()
quiz_list.clear()
speaker2 = pyttsx3.init()
speaker2.say(end_text)
speaker2.say("Click the start button,to play again")
speaker2.runAndWait()
if actual_score > best_score:
best_score = actual_score
best_score_final.set(best_score)
break
# labels:
label_best = tk.Label(root,text="best score: ",textvariable=best_score_final,fg="#900C3F")
label_score = tk.Label(root,text="actual score: ",textvariable=actual_score_actual,fg="#07A0F1")
label_best2 = tk.Label(root,fg="#900C3F")
label_score2 = tk.Label(root,fg="#07A0F1")
label_input = tk.Label(root,textvariable=guess)
# define buttons:
button_start = Button(root,text="Start",command=game,bg="#65B9E5")
button_0 = Radiobutton(root,text="with sound",variable=sound_or_color,value=1)
button_10 = Radiobutton(root,text="with color",value=2)
button_1 = Button(root,text="1",padx=40,pady=20,command=lambda: button_click(1))
button_2 = Button(root,text="2",command=lambda: button_click(2))
button_3 = Button(root,text="3",command=lambda: button_click(3))
button_4 = Button(root,text="4",command=lambda: button_click(4))
button_5 = Button(root,text="5",command=lambda: button_click(5))
button_6 = Button(root,text="6",command=lambda: button_click(6))
button_7 = Button(root,text="7",command=lambda: button_click(7))
button_8 = Button(root,text="8",command=lambda: button_click(8))
button_9 = Button(root,text="9",command=lambda: button_click(9))
# Put buttons on the screen
label_input.grid(row=0,column=0,columnspan=3)
button_1.grid(row=1,column=0)
button_2.grid(row=1,column=1)
button_3.grid(row=1,column=2)
button_4.grid(row=2,column=0)
button_5.grid(row=2,column=1)
button_6.grid(row=2,column=2)
button_7.grid(row=3,column=0)
button_8.grid(row=3,column=1)
button_9.grid(row=3,column=2)
button_0.grid(row=4,column=0)
button_10.grid(row=4,column=1)
button_start.grid(row=4,column=2)
label_score2.grid(row=5,column=0)
label_best2.grid(row=5,column=1)
label_score.grid(row=6,column=0)
label_best.grid(row=6,column=1)
root.mainloop()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)