【发布时间】: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