th函数用计算机怎么按出来,从Tkinter框架调用函数到anoth

我有一个页面,我将显示一些客户的详细信息。因此,我创建了一个名为“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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值