我有一个页面,我将显示一些客户的详细信息。因此,我创建了一个名为“customers details”的页面,其中包含我需要的所有标签,并将这些标签的文本设置为变量。可惜没用。标签是在__init__方法中创建的,所以我不能“更新”它们,因为init只在开头被调用。所以我的想法是创建一个包含所有标签的新函数,当我有必要的时候,我会调用这个函数……问题就在这里。我无法调用另一个tk.Frame中的函数。以下代码是代码的简化版本。在import tkinter as tk
from tkinter import ttk
class Myapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = ttk.Frame(self, borderwidth=10, relief="sunken", width=200, height=100)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (HomePage, PageOne):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="N,S,E,W")
self.show_frame(HomePage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class HomePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = ttk.Label(self, text="HomePage")
label.pack()
button1 = ttk.Button(self, text="Quit",
command=lambda: quit())
button1.pack()
button2 = ttk.Button(self, text="Call Function in the other page/class to show the label",
command=lambda: PageOne.function()) # this is to do it from an other class. I can't do this
button2.pack()
button3 = ttk.Button(self, text="Page One",
command=lambda: controller.show_frame(PageOne))
button3.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = ttk.Label(self, text="PageOne")
label.pack()
button1 = ttk.Button(self, text="Quit",
command=lambda: quit())
button1.pack()
button2 = ttk.Button(self, text="Call Function, in local it works..",
command=lambda: self.function())#this is to do it in local
button2.pack()
button3 = ttk.Button(self, text="HomePage",
command=lambda: controller.show_frame(HomePage))
button3.pack()
def function(self):
label1 = ttk.Label(self, text="It Worked!")
label1.pack()
app = Myapp()
app.mainloop()