如何在课程MainPage
中使用课程AddPart
中的某个功能?我在类Mainpage
中有一些函数,我想在另一个类MainPage
中使用类AddPart
中的所有函数。
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')
答案 0 :(得分:0)
您是否尝试继承基类?
class AddPart(MainPage):
而不是class AddPart(tk.Frame, MainPage):
,因为MainPage
已经是tk.Frame
。
答案 1 :(得分:0)
我想在另一个中使用MainPage类中的所有函数 class AddPart
这就是继承的用途。正如@progmatico和@mondlos指出的那样,通过继承MainPage
中的AddPart
,您将可以访问所有基类函数。
看起来你的两个类都有依赖于类的函数(除非我误解了你正在使用的tk
模块),所以使用类似静态方法的东西不会很好。
在您的评论中,您在尝试@mondlos建议时收到TypeError: Cannot create a consistent method resolution order (MRO) for bases
错误。这是因为您从[{1}}继承了tk.Frame
中包含的MainPage
类型AddPart
。做@progmatico建议应该工作。有几个答案表明双重继承导致了这个问题:TypeError: Cannot create a consistent method resolution order (MRO)