我目前正在使用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”变量来解决此问题?有什么想法吗?
答案 0 :(得分:0)
constructGUI
是在main
中定义的,因此无法在该功能之外访问。
通过在全局范围内的函数外部放置占位符,使constructGUI
成为全局变量。如果您需要在一个函数中进行修改(看起来像您的样子),请确保在该函数的顶部添加global constructGUI
。
答案 1 :(得分:0)
{<form action="edit_product.php" method="post" id="edit_product_form">
...
<input type="hidden" name="productID" value="<?php echo $product_id; ?>">
</form>
仅在constructGUI
范围内存在。您不会将其传递给其他功能,以使他们不知道它是什么。
使main()
为模块级别的对象,以便每个人都能看到它。
constructGUI
文档中有一些内容需要阅读:Naming and Binding,A Word About Names and Objects and Python Scopes and Namespaces。