简体   繁体   中英

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. 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.

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

song1name is a local variable to the newsongs function. You need to declare the song1name and the other song#name variables in a global context, or else store it elsewhere.

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.

You might have a better result declaring a songnames = [] list, either declared outside of any function or passed into submit as an argument. Then set the songs you choose as the four items in it, and access those later in the 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. You can declare them as global to fix the issue.

However you don't need those songXname at all, just use 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_)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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