简体   繁体   English

使用 tkinter 中的按钮生成一个新列表,其中稍后使用变量错误 python tkinter

[英]generate a new list with a button in tkinter with variables used later error python tkinter

I have created a program which picks 4 random songs from a list when you press the 'New List' button.我创建了一个程序,当您按下“新列表”按钮时,它会从列表中随机选择 4 首歌曲。 These songs will then be assigned to variables and the 'Play Song' buttons will play the song and you will have to enter your guess and press submit.然后将这些歌曲分配给变量,“播放歌曲”按钮将播放歌曲,您必须输入您的猜测并按提交。

Now I have defined the command 'newlist' and it works fine it make a new list of the songs from the main list but the problem is I cannot carry the song name generated from that random.sample later on in the program.现在我已经定义了命令“newlist”,它工作正常,它从主列表中创建了一个新的歌曲列表,但问题是我不能在程序中携带从random.sample生成的歌曲名称。

Full code:完整代码:

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()



When running this I get an error File "v2.5.py", line 73, in submit check(guess1,song1name,_status1_) NameError: name 'song1name' is not defined运行此程序时,我收到错误File "v2.5.py", line 73, in submit check(guess1,song1name,_status1_) NameError: name 'song1name' is not defined

song1name is a local variable to the newsongs function. song1name 是新闻歌曲newsongs的局部变量。 You need to declare the song1name and the other song#name variables in a global context, or else store it elsewhere.您需要在全局上下文中声明 song1name 和其他 song#name 变量,或者将其存储在其他地方。

You can initialize them all at the start of the file with something like song1name = song2name = song3name = song4name = None , but this is a long way to go about it.您可以在文件的开头使用类似song1name = song2name = song3name = song4name = None类的东西来初始化它们,但这对于 go 来说还有很长的路要走。

You might have a better result declaring a songnames = [] list, either declared outside of any function or passed into submit as an argument.声明一个songnames = []列表可能会得到更好的结果,要么在任何 function 之外声明,要么作为参数传递给submit Then set the songs you choose as the four items in it, and access those later in the submit function.然后将您选择的歌曲设置为其中的四个项目,然后在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.

It is because those songXname are local variables inside newsongs() function which cannot be accessed outside the function.这是因为这些songXnamenewsongs() function 中的局部变量,在 function 之外无法访问。 You can declare them as global to fix the issue.您可以将它们声明为global以解决问题。

However you don't need those songXname at all, just use songs :但是,您根本不需要那些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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM