我有一个页面,将在其中显示一些客户详细信息。因此,我创建了一个名为“客户详细信息”的页面,其中包含我需要的所有标签,并将这些标签的文本设置为变量。可惜它不起作用。标签创建于__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()