[英]generate a new list with a button in tkinter with variables used later error python tkinter
我创建了一个程序,当您按下“新列表”按钮时,它会从列表中随机选择 4 首歌曲。 然后将这些歌曲分配给变量,“播放歌曲”按钮将播放歌曲,您必须输入您的猜测并按提交。
现在我已经定义了命令“newlist”,它工作正常,它从主列表中创建了一个新的歌曲列表,但问题是我不能在程序中携带从random.sample
生成的歌曲名称。
完整代码:
from playsound import playsound
import random
import tkinter as tk
from tkinter import *
master=tk.Tk()
master.title("Song Guessing Game")
master.config(bg="royalblue1")
background="royalblue1"
background2="ghost white"
background3="blue2"
bg4="steelblue"
#SONG LISTS:
songs=["big chungus.mp3","candyland.mp3","a92 fumez.mp3","on and on.mp3","troll song.mp3"]
def newsongs():
newsongs=random.sample(songs,4)
song1,song2,song3,song4=newsongs
song1name=song1[:-4].upper()
song2name=song2[:-4].upper()
song3name=song3[:-4].upper()
song4name=song4[:-4].upper()
print("Song List:",song1,song2,song3,song4)
tk.Button(master,text="New Songs",bg=bg4,relief="solid",fg="black",command=newsongs).grid()
def play1():
playsound(song1)
def play2():
playsound(song2)
def play3():
playsound(song3)
def play4():
playsound(song4)
tk.Label(master,text="Song Guesser",font="Verdanda 20 underline bold",bg=background,fg="black",relief="solid").grid(row=0,column=1)
tk.Button(master,text="Play Song 1",command=play1,font="Verdanda 15 bold",bg=bg4).grid(row=1,column=0)
tk.Button(master,text="Play Song 2",command=play2,font="Verdanda 15 bold",bg=bg4).grid(row=2,column=0)
tk.Button(master,text="Play Song 3",command=play3,font="Verdanda 15 bold",bg=bg4).grid(row=3,column=0)
tk.Button(master,text="Play Song 4",command=play4,font="Verdanda 15 bold",bg=bg4).grid(row=4,column=0)
guess1=tk.Entry(master,bg=background3,fg="black",font="Verdanda 15")
guess2=tk.Entry(master,bg=background3,fg="black",font="Verdanda 15")
guess3=tk.Entry(master,bg=background3,fg="black",font="Verdanda 15")
guess4=tk.Entry(master,bg=background3,fg="black",font="Verdanda 15")
guess1.grid(row=1,column=1)
guess2.grid(row=2,column=1)
guess3.grid(row=3,column=1)
guess4.grid(row=4,column=1)
def submit():
def check(guess,expected,label):
if guess.get().strip().upper()==expected:
label.config(text="Correct",fg="green")
else:
label.config(text="Incorrect",fg="red")
check(guess1,song1name,_status1_)
check(guess2,song2name,_status2_)
check(guess3,song3name,_status3_)
check(guess4,song4name,_status4_)
_status1_=tk.Label(master,font="Verdanda 15 bold",bg=background2)
_status2_=tk.Label(master,font="Verdanda 15 bold",bg=background2)
_status3_=tk.Label(master,font="Verdanda 15 bold",bg=background2)
_status4_=tk.Label(master,font="Verdanda 15 bold",bg=background2)
_status1_.grid(row=1,column=3)
_status2_.grid(row=2,column=3)
_status3_.grid(row=3,column=3)
_status4_.grid(row=4,column=3)
_status1_.config()
tk.Button(master,text="Submit Score",command=submit).grid(row=5,column=1)
master.mainloop()
运行此程序时,我收到错误File "v2.5.py", line 73, in submit check(guess1,song1name,_status1_) NameError: name 'song1name' is not defined
song1name 是新闻歌曲newsongs
的局部变量。 您需要在全局上下文中声明 song1name 和其他 song#name 变量,或者将其存储在其他地方。
您可以在文件的开头使用类似song1name = song2name = song3name = song4name = None
类的东西来初始化它们,但这对于 go 来说还有很长的路要走。
声明一个songnames = []
列表可能会得到更好的结果,要么在任何 function 之外声明,要么作为参数传递给submit
。 然后将您选择的歌曲设置为其中的四个项目,然后在submit
function 中访问这些歌曲。
songlist = []
def newsongs():
songlist = [song[:4].upper() for song in random.sample(songs,4)]
...
check(guess1,songlist[0],_status1_)
check(guess1,songlist[1],_status2_)
etc.
这是因为这些songXname
是newsongs()
function 中的局部变量,在 function 之外无法访问。 您可以将它们声明为global
以解决问题。
但是,您根本不需要那些songXname
,只需使用songs
:
def newsongs():
random.shuffle(songs) # shuffle the song list randomly
print("Song List:", songs[:4])
...
def play1():
playsound(songs[0])
def play2():
playsound(songs[1])
def play3():
playsound(songs[2])
def play4():
playsound(songs[3])
...
def submit():
def check(guess,expected,label):
expected = expected.split(".")[0].upper()
if guess.get().strip().upper()==expected:
label.config(text="Correct",fg="green")
else:
label.config(text="Incorrect",fg="red")
check(guess1,songs[0],_status1_)
check(guess2,songs[1],_status2_)
check(guess3,songs[2],_status3_)
check(guess4,songs[3],_status4_)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.