gpt4 book ai didi

python - 尝试更改外部函数中的类变量时出现 NameError

转载 作者:太空宇宙 更新时间:2023-11-03 21:29:24 25 4
gpt4 key购买 nike

我目前正在用 python 开发一个点唱机/音乐播放器。为了让代码更有条理,我把它安排到了具体的类中。一些外部函数需要访问类变量并更新它们。但是,该程序会抛出以下错误:

 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1702, in __call__
return self.func(*args)
File "jukeboxDraft.py", line 77, in nextSong
constructGUI.index += 1
NameError: name 'constructGUI' is not defined

类和访问类变量的函数位于以下代码块中:

import os
import pygame
from tkinter.filedialog import askdirectory
from tkinter import *
import eyed3

class JukeboxContent:
def __init__(self):
listOfSongs = []
songTitles = []
directory = askdirectory()
self.listOfSongs = listOfSongs
self.songTitles = songTitles
self.directoryAsk = directory
self.Error_NoMP3s = "No \".mp3\" files found."

def directoryChooser(self):
self.directoryAsk
os.chdir(self.directoryAsk)

for files in os.listdir(self.directoryAsk):
if files.endswith(".mp3"):
realdir = os.path.realpath(files)
audioTag = eyed3.load(realdir)
self.songTitles.append(audioTag.tag.title)
self.listOfSongs.append(files)
#print(files)

pygame.mixer.init()
pygame.mixer.music.load(self.listOfSongs[0])
pygame.mixer.music.play()



class JukeboxGUI(JukeboxContent):
index = 0
def __init__(self, window):
JukeboxContent.__init__(self)
self.back = Frame(master = window, width=500, height=500, bg='pink')
self.label = Label(window, text = "Jukebox")
self.listBox = Listbox(window)
self.nextButton = Button(window, text = "Next Song")
self.previousButton = Button(window, text = "Previous Song")
self.stopButton = Button(window, text = "Stop")
self.labelVar = StringVar()
self.songLabel= Label(window, textvariable = self.labelVar, width = 50)

def constructButtons(self):
self.back.pack()
self.label.pack()
self.listBox.pack()
for items in self.listOfSongs:
self.listBox.insert(END, items)
self.nextButton.pack()
self.previousButton.pack()
self.stopButton.pack()

def updateLabel(self):
self.labelVar.set(self.songTitles[self.index])

#-------------------------JUKEBOX FUNCTIONS-------------------------------------


def main():
window = Tk()
window.title("Jukebox")
initiateJukebox = JukeboxContent()
initiateJukebox.directoryChooser()
constructGUI = JukeboxGUI(window)
constructGUI.constructButtons()
constructGUI.nextButton.bind("<Button-1>", nextSong)
constructGUI.previousButton.bind("<Button-1>", previousSong)
constructGUI.stopButton.bind("<Button-1>", stopSong)
window.mainloop()

def nextSong(event):
constructGUI.index += 1
pygame.mixer.music.load(initiateJukebox.listOfSongs[constructGUI.index])
pygame.mixer.music.play()
constructGUI.updateLabel()

def previousSong(event):
constructGUI.index -= 1
pygame.mixer.music.load(initiateJukebox.listOfSongs[constructGUI.index])
pygame.mixer.music.play()
constructGUI.updateLabel()

def stopSong(event):
pygame.mixer.music.stop()
constructGUI.labelVar.set("")



if __name__ == '__main__':
main()

我应该如何编辑“JukeboxGUI”类或“constructGUI”变量来解决这个问题?有什么想法吗?

最佳答案

constructGUI 是在 main 中定义的,因此在该函数之外无法访问它。

通过在全局范围内的函数外部放置占位符,使 constructGUI 成为全局变量。如果您需要在函数内修改它(看起来就是这样),请确保将 global ConstructionGUI 添加到该函数的顶部。

关于python - 尝试更改外部函数中的类变量时出现 NameError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53624265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com