不太确定如何表达这一点,认为我可能是愚蠢的,但我在main()函数中声明变量albumInfoArray。有没有办法从我的主屏幕类访问这个。例如,skip_button_clicked()方法需要访问albumInfoArray中的值,而这个数组似乎不在它的作用域内。
class MainScreen:
def __init__(self):
self.main_screen = Tk()
self.main_screen.geometry ('500x600+100+100')
self.main_screen.title("Album")
self.main_screen.configure(background = 'aliceblue')
self.albumTemp = Image.open("/Users/peacockben/Documents/Ben/Album Guesser/CurrentImage.jpeg")
self.albumTemp = self.albumTemp.resize((250,250), Image.ANTIALIAS)
self.album = ImageTk.PhotoImage(self.albumTemp)
self.create_widgets()
def create_widgets(self):
self.pad1 = Label(self.main_screen, text = " ", bg = "blue", height = 2,width = 13)
self.pad1.grid(row = 0, column = 0)
self.Title = Label(self.main_screen, text = "Albumle",bg ="blue", font = ('',36, 'bold'))
self.Title.grid(row = 1, column = 1)
self.AlbumImg = Label(self.main_screen, image = self.album, bg = 'aliceblue')
self.AlbumImg.grid(row = 2, column = 1)
self.skip_button = Button(self.main_screen,text = "Skip", height = "2",width = "15")
self.skip_button.grid(row = 3, column = 1)
def skip_button_clicked(self):
print(albumInfoArray)
def main():
albumInfoArray = get_album_info(get_album_index())
blur_image(albumInfoArray[2],50)
program = MainScreen()
program.main_screen.mainloop()
albumInfoArray
在main函数的作用域中,不应该在它的外部被访问。
您可以尝试将其声明为全局:
def main():
global albumInfoArray
albumInfoArray = get_album_info(get_album_index())
etc...
在main()中声明的变量只能在main中访问。您甚至不能在main()所在的同一个类中访问它。您可以尝试将该变量声明为与main相同的类中的全局变量。