【问题标题】:How to use functions from one class to another in python tkinter如何在 python tkinter 中使用从一个类到另一个类的函数
【发布时间】:2018-03-19 03:05:28
【问题描述】:

如何在AddPart 类中使用MainPage 类中的函数?我在Mainpage 类中有一些函数,我想在另一个类AddPart 中使用MainPage 类中的所有函数。

class MainPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent) #initialise parent class --> TSS()


    def main_heading(self, heading):
        main_label = tk.Label(self, text = heading, font = LARGE_FONT, fg = 'white', bg = 'RoyalBlue1', width = 1000, height = 4)
        main_label.pack(side = 'top')

    def sub_heading(self, title):
        sub_label = tk.Label(self, text = title, font = MED_FONT, fg = 'RoyalBlue2')
        sub_label.pack(side = 'top', padx = 20, pady = 20)

    def paragraph(self, para):
        paragraph_label = tk.Label(self, text = para, font = SMALL_FONT, fg = 'gray26')
        paragraph_label.pack(side = 'top', padx = 20, pady = 20)

    def top_tool(self):
        toolbar = tk.Frame(self, bg = 'RoyalBlue2')
        AddBtn = tk.Button(toolbar, text = 'ADD', width = 10, bg = 'RoyalBlue2', fg = 'white', borderwidth=0)
        AddBtn.pack(side = 'left', padx = 0, pady = 0)
        RemBtn = tk.Button(toolbar, text = 'REMOVE', width = 10, bg = 'RoyalBlue2', fg = 'white', borderwidth=0)
        RemBtn.pack(side = 'left', padx = 0, pady = 0)
        RevBtn = tk.Button(toolbar, text = 'REVIEW', width = 10, bg = 'RoyalBlue2', fg = 'white', borderwidth=0)
        RevBtn.pack(side = 'left', padx = 0, pady = 0)
        AssignBtn = tk.Button(toolbar, text = 'ASSIGN EVENTS', width = 15, bg = 'RoyalBlue2', fg = 'white', borderwidth=0)
        AssignBtn.pack(side = 'left', padx = 0, pady = 0)
        toolbar.pack(side = 'top', fill = 'x')

#-----Second Class------
class AddPart(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.top_tool()
        self.main_heading("Home")

    def main_heading(self, heading):
        main_label = tk.Label(self, text = heading, font = LARGE_FONT, fg = 'white', bg = 'RoyalBlue1', width = 1000, height = 4)
        main_label.pack(side = 'top')

【问题讨论】:

  • 你尝试继承基类吗? class AddPart(tk.Frame, MainPage)
  • 它给了我一个错误:回溯(最近一次调用最后一次):文件“C:\Users\maste\Desktop\Python\App\launch_tss.py”,第 102 行,在 类中AddPart(tk.Frame,MainPage):TypeError:无法为基础Frame,MainPage创建一致的方法解析顺序(MRO)>>>

标签: python function class tkinter


【解决方案1】:

你有没有尝试继承基类? class AddPart(MainPage):

而不是class AddPart(tk.Frame, MainPage):,因为MainPage 已经是tk.Frame。

【讨论】:

    【解决方案2】:

    Inheritance

    我想在另一个中使用 MainPage 类中的所有函数 类 AddPart

    这正是继承的用途。正如@progmatico 和@mondlos 所指出的,通过在AddPart 中继承MainPage,您将可以访问所有基类函数。

    看起来您的两个类都具有依赖于该类的功能(除非我误解了您正在使用的 tk 模块),因此使用静态方法之类的方法效果不佳。

    根据您的评论,您在尝试 @mondlos 建议时收到 TypeError: Cannot create a consistent method resolution order (MRO) for bases 错误。这是因为您从MainPage 继承了tk.Frame 类型,您也将其包含在AddPart 中。做@progmatico 推荐的应该是可行的。在这个 SO 问题上有几个答案表明双重继承会导致这个问题:TypeError: Cannot create a consistent method resolution order (MRO)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多