在学校的计算机课上,我们必须为电影亭开发软件。我有一个概述每个屏幕基础的类(class),然后从该类(class)中我为每个单独的屏幕制作单独的类(class)。当用户单击一个按钮时,我希望屏幕上的一堆小部件更新以显示相关信息。这是显示电影的屏幕类。
class BrowseMovies(tk.Frame):
def switchMovie():
global movNum
movNum += 1
print(movNum)
#update the gui
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
backGroundPicture = ImageTk.PhotoImage(Image.open("Background.jpg"))
backGroundLabel = tk.Label(self, image=backGroundPicture, borderwidth=0)
backGroundLabel.image = backGroundPicture
backGroundLabel.place(x=0, y=0, anchor=tk.NW)
moviesLabel = tk.Label(self, text="Movies", font=("Verdana", 48), bg="#001f33", fg="red")
moviesLabel.place(relx=0.5, rely=0, anchor=tk.N)
middleFrame = tk.Frame(self)
middleFrame.place(relx=0.5, rely=0.5, anchor=tk.CENTER, height=470, width=750)
movieImage = ImageTk.PhotoImage(Image.open(moviePosterLinks[movNum]))
movieImageLabel = tk.Label(middleFrame, image=movieImage, borderwidth=0)
movieImageLabel.image = movieImage
movieImageLabel.place(x=0, rely=0.5, anchor=tk.W)
movieTitleLabel = tk.Label(middleFrame, text=movieTitles[movNum], font=("Verdana", 20), borderwidth=0)
movieTitleLabel.place(x=317, y=0, anchor=tk.NW)
movieRatingsLabel = tk.Label(middleFrame, text="Rotten Tomatoes Rating: " + movieRatings[movNum], font=("Verdana", 20), borderwidth=0)
movieRatingsLabel.place(x=317, y=30, anchor=tk.NW)
movieRestrictionsLabel = tk.Label(middleFrame, text=movieRestrictions[movNum], font=("Verdana", 20), borderwidth=0)
movieRestrictionsLabel.place(x=317, y=60, anchor=tk.NW)
movieDirectorsLabel = tk.Label(middleFrame, text="Director: " + movieDirectors[movNum], font=("Verdana", 20), borderwidth=0)
movieDirectorsLabel.place(x=317, y=90, anchor=tk.NW)
movieGenresLabel = tk.Label(middleFrame, text="Genre: " + movieGenres[movNum], font=("Verdana", 20), borderwidth=0)
movieGenresLabel.place(x=317, y=120, anchor=tk.NW)
returnButton = tk.Button(self, text="Return to Main Menu", bg="#4db8ff", fg="black",
command=lambda: controller.show_frame(StartPage))
returnButton.place(relx=1, rely=1, anchor=tk.SE)
changeButton = tk.Button(self, text="Switch to next movie", command=lambda: BrowseMovies.switchMovie(controller))
changeButton.place(relx=1, rely=0.5, anchor=tk.E)
当我调用 switchMovies 函数时,movNum 变量(代表当前选定的电影)会更新,但是 gui 保持空闲状态,我如何让它更新以显示新信息?
*********/****编辑****/********
现在按照建议为模板“Window”类添加代码。
class Window(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title("Movie Kiosk")
self.attributes("-fullscreen", True)
self.resizable(width=False, height=False)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=1)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for f in (StartPage, CheckOut, BrowseMovies, BrowseSnacks):
frame = f(container, self)
self.frames[f] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
请您参考如下方法:
我建议使用更简单的名称,不要使用多余的“电影”和“标签”。但无论如何,由于可以从电影编号设置标签,因此定义更新标签的方法,并将其用于初始设置。我假设在您发布的代码之前将 movNum 初始化为 0。换句话说,
class BrowseMovies(tk.Frame):
...
def __init__(self, parent, controller):
...
title = tk.Label(middleFrame, font=("Verdana", 20), borderwidth=0)
...
self.update_labels() # after all labels defined
...
def update_labels(self):
self.title['text'] = movieTitles[movNum]
... # similar for other titles
然后当 movNum 改变时也调用 update_labels。
