簡體   English   中英

使用 tkinter 中的按鈕生成一個新列表,其中稍后使用變量錯誤 python tkinter

[英]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.

這是因為這些songXnamenewsongs() 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM