【问题标题】:tkinter : how to call a method of a child from another childtkinter:如何从另一个孩子调用一个孩子的方法
【发布时间】:2013-09-15 17:15:56
【问题描述】:

这是我第一次使用pyhton创建图形界面,我很难理解父母和孩子之间的联系。

我首先在一个单独的线程中创建根窗口:

import tkinter as tk
import threading as th

class GUI(th.Thread):
    def __init__(self):
        th.Thread.__init__(self)
        self.root = tk.Tk()
        self.gp = MainFrame(self.root)

    def start(self):
        self.root.mainloop()

    def stop(self):
        self.root.destroy()

这个类的构造函数调用主框架的构造函数:

class MainFrame(tk.Frame):
    def __init__(self,parent):
        tk.Frame.__init__(self,parent)
        self.x=my_label(self)
        self.up=my_update(self)
        self.grid()

在这个主框架中,我只有两个简单的元素,它们的类是:

class my_label(tk.Frame):
    def __init__(self,parent):
        tk.Frame.__init__(self,parent)
        self.l = tk.Label(self,text="some text")
        self.l.grid()
        self.grid()

class my_update(tk.Frame):
    def __init__(self,parent):
        tk.Frame.__init__(self,parent)

        self.b=tk.Button(self,text="update",command=self.ButtonPushed)
        self.b.grid()
        self.grid()

    def ButtonPushed(self):
        # here is my problem...

这样做可行,但是当我单击更新按钮时,我想修改x(在MainFrame 中)。换句话说,我想从一个孩子修改另一个父母的孩子……我该怎么做?

附带问题:我的图形界面的总体构造是否正确?我的意思是,让my_updatemy_labeltk.Frame 继承是否正确?

【问题讨论】:

    标签: python class inheritance tkinter


    【解决方案1】:

    尝试以下操作:(查看评论以查看我修改的内容。)

    class MainFrame(tk.Frame):
        def __init__(self,parent):
            tk.Frame.__init__(self,parent)
            self.x=my_label(self)
            self.up=my_update(self, self.x) # <-- pass another child to constructor
            self.grid()
    

    class my_label(tk.Frame):
        def __init__(self,parent):
            tk.Frame.__init__(self,parent)
            self.l = tk.Label(self,text="some text")
            self.l.grid()
            self.grid()
    
        # my_update.ButtonPushed will call this method.
        def ButtonPushed(self):
            self.l['text'] = 'pushed'
    

    class my_update(tk.Frame):
        def __init__(self,parent, friend):
            #                     ^^^^^^
            # accept another child reference as `freind` as save it as self.friend
            tk.Frame.__init__(self,parent)
            self.friend = friend # <-- save reference to another child
    
            self.b=tk.Button(self,text="update",command=self.ButtonPushed)
            self.b.grid()
            self.grid()
    
        def ButtonPushed(self):
            self.friend.ButtonPushed() # <-- call another child's method freely.
    

    【讨论】:

    • 谢谢,你的方法有效。但是当我阅读你的答案时,我意识到我的程序的概念是不正确的......所以我不需要你的解决方案。无论如何,你通过向我展示我可以找到更好的编码方式来帮助我!
    【解决方案2】:

    对于您的附带问题:我不确定您的应用程序还需要做什么,所以也许有一个很好的理由,但在我看来,将标签和按钮包装在一个继承自 Frame 的额外类中过度使事情复杂化了一点。我想我会这样做:

    class MainFrame(tk.Frame):
        def __init__(self,parent):
            tk.Frame.__init__(self,parent)
            self.x=tk.Label(self, text="some text")
            self.up=tk.Button(self, text="update", command=self.ButtonPushed)
            self.x.grid()
            self.up.grid()
            self.grid()
    
        def ButtonPushed(self):
            # do something with self.x
    

    或者如果你真的需要额外的类,为什么不让子类继承自 tk.Label 和 tk.Button。无需将它们包装在额外的框架中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多