多重继承在 python 中不起作用
Multiple inheritance not working in python
我正在做一个 GUI 格式的 bmi 计算器作为学校项目。
这是代码:
from tkinter import *
import tkinter.font as tkfont
class App(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
#Setup menu here
MainMenu(self)
# Setup Frame
container = Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo, ResultPageOne, ResultPageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, context):
frame = self.frames[context]
frame.tkraise()
class StartPage(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
fontstyle = tkfont.Font(family="Helvetica", size=30)
label = Label(self, text="BMI CALCULATOR",font=fontstyle)
label.pack(padx=10, pady=10)
go_to_metric = Button(self, text="Measure in Metric values", command=lambda:controller.show_frame(PageOne), width=30)
go_to_metric.pack(side=LEFT)
go_to_english = Button(self, text="Measure in English values", command=lambda:controller.show_frame(PageTwo), width=30)
go_to_english.pack(side=LEFT)
class PageOne(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = Label(self, text="Measure BMI in metric values")
label.pack(padx=10, pady=10)
Label(self, text="Height in centimeters (decimal points allowed):").pack()
height_in_cm = Entry(self)
height_in_cm.pack()
Label(self, text="Weight in Kilograms (decimal points allowed)").pack()
weight_in_kg = Entry(self)
weight_in_kg.pack()
height_in_cm = height_in_cm.get()
weight_in_kg = weight_in_kg.get()
Button(self, text="Calculate", command=lambda:controller.show_frame(ResultPageOne)).pack()
page_two = Button(self, text="Measure in English values", command=lambda:controller.show_frame(PageTwo))
page_two.pack()
def calculate_bmi(self):
height_in_m = self.height_in_cm/100
final_height = height_in_m**2
bmi = self.weight_in_kg/final_height
return bmi
class PageTwo(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = Label(self, text="Measure BMI in English values")
label.pack(padx=10, pady=10)
Label(self, text="Height in Inches (decimal points allowed):").pack()
height_in_in = Entry(self)
height_in_in.pack()
Label(self, text="Weight in Pounds (decimal points allowed)").pack()
weight_in_lbs = Entry(self)
weight_in_lbs.pack()
height_in_in = height_in_in.get()
weight_in_lbs = weight_in_lbs.get()
Button(self, text="Calculate", command=lambda:controller.show_frame(ResultPageTwo)).pack()
page_one = Button(self, text="Measure in Metric values", command=lambda:controller.show_frame(PageOne))
page_one.pack()
def calculate_bmi(self):
final_height = self.height_in_in**2
bmi = self.weight_in_lbs/final_height*703
return bmi
class ResultPageOne(Frame, PageOne):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
fontstyle = tkfont.Font(family="Helvetica", size=30)
label = Label(self,text="Results", font=fontstyle)
label.pack()
class ResultPageTwo(Frame, PageTwo):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
fontstyle = tkfont.Font(family="Helvetica", size=30)
label = Label(self, text="Results 2", font=fontstyle)
label.pack()
class MainMenu:
def __init__(self,master):
menubar = Menu(master)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Exit", command=master.quit)
menubar.add_cascade(label="File",menu=filemenu)
master.config(menu=menubar)
app = App()
app.mainloop()
我想在 classes 即 PageOne 和 PageTwo 中采用 calculate_bmi 方法并将它们继承到两个结果页面 classes 中并显示结果。
但是当我运行代码的时候,出现了这个错误:
回溯(最后一次调用):
文件“TN1.py”,第 92 行,位于
class 结果页面一(框架,页面一):
类型错误:无法创建一致的方法解析
基础 Frame、PageOne
的订单 (MRO)
这种情况在section Bad Method Resolution Orders
中有描述
A MRO is bad when it breaks such fundamental properties as local precedence ordering and monotonicity.
>>> F=type('Food',(),{'remember2buy':'spam'})
>>> E=type('Eggs',(F,),{'remember2buy':'eggs'})
>>> G=type('GoodFood',(F,E),{}) # under Python 2.3 this is an error!
with inheritance diagram
O
|
(buy spam) F
| \
| E (buy eggs)
| /
G
(buy eggs or spam ?)
The real solution is to design a non-ambiguous hierarchy, i.e. to derive G from E and F (the more specific first) and not from F and E; in this case the MRO is GEF without any doubt.
我正在做一个 GUI 格式的 bmi 计算器作为学校项目。
这是代码:
from tkinter import *
import tkinter.font as tkfont
class App(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
#Setup menu here
MainMenu(self)
# Setup Frame
container = Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo, ResultPageOne, ResultPageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, context):
frame = self.frames[context]
frame.tkraise()
class StartPage(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
fontstyle = tkfont.Font(family="Helvetica", size=30)
label = Label(self, text="BMI CALCULATOR",font=fontstyle)
label.pack(padx=10, pady=10)
go_to_metric = Button(self, text="Measure in Metric values", command=lambda:controller.show_frame(PageOne), width=30)
go_to_metric.pack(side=LEFT)
go_to_english = Button(self, text="Measure in English values", command=lambda:controller.show_frame(PageTwo), width=30)
go_to_english.pack(side=LEFT)
class PageOne(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = Label(self, text="Measure BMI in metric values")
label.pack(padx=10, pady=10)
Label(self, text="Height in centimeters (decimal points allowed):").pack()
height_in_cm = Entry(self)
height_in_cm.pack()
Label(self, text="Weight in Kilograms (decimal points allowed)").pack()
weight_in_kg = Entry(self)
weight_in_kg.pack()
height_in_cm = height_in_cm.get()
weight_in_kg = weight_in_kg.get()
Button(self, text="Calculate", command=lambda:controller.show_frame(ResultPageOne)).pack()
page_two = Button(self, text="Measure in English values", command=lambda:controller.show_frame(PageTwo))
page_two.pack()
def calculate_bmi(self):
height_in_m = self.height_in_cm/100
final_height = height_in_m**2
bmi = self.weight_in_kg/final_height
return bmi
class PageTwo(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = Label(self, text="Measure BMI in English values")
label.pack(padx=10, pady=10)
Label(self, text="Height in Inches (decimal points allowed):").pack()
height_in_in = Entry(self)
height_in_in.pack()
Label(self, text="Weight in Pounds (decimal points allowed)").pack()
weight_in_lbs = Entry(self)
weight_in_lbs.pack()
height_in_in = height_in_in.get()
weight_in_lbs = weight_in_lbs.get()
Button(self, text="Calculate", command=lambda:controller.show_frame(ResultPageTwo)).pack()
page_one = Button(self, text="Measure in Metric values", command=lambda:controller.show_frame(PageOne))
page_one.pack()
def calculate_bmi(self):
final_height = self.height_in_in**2
bmi = self.weight_in_lbs/final_height*703
return bmi
class ResultPageOne(Frame, PageOne):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
fontstyle = tkfont.Font(family="Helvetica", size=30)
label = Label(self,text="Results", font=fontstyle)
label.pack()
class ResultPageTwo(Frame, PageTwo):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
fontstyle = tkfont.Font(family="Helvetica", size=30)
label = Label(self, text="Results 2", font=fontstyle)
label.pack()
class MainMenu:
def __init__(self,master):
menubar = Menu(master)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Exit", command=master.quit)
menubar.add_cascade(label="File",menu=filemenu)
master.config(menu=menubar)
app = App()
app.mainloop()
我想在 classes 即 PageOne 和 PageTwo 中采用 calculate_bmi 方法并将它们继承到两个结果页面 classes 中并显示结果。 但是当我运行代码的时候,出现了这个错误:
回溯(最后一次调用): 文件“TN1.py”,第 92 行,位于 class 结果页面一(框架,页面一): 类型错误:无法创建一致的方法解析 基础 Frame、PageOne
的订单 (MRO)这种情况在section Bad Method Resolution Orders
中有描述A MRO is bad when it breaks such fundamental properties as local precedence ordering and monotonicity.
>>> F=type('Food',(),{'remember2buy':'spam'}) >>> E=type('Eggs',(F,),{'remember2buy':'eggs'}) >>> G=type('GoodFood',(F,E),{}) # under Python 2.3 this is an error!
with inheritance diagram
O | (buy spam) F | \ | E (buy eggs) | / G (buy eggs or spam ?)
The real solution is to design a non-ambiguous hierarchy, i.e. to derive G from E and F (the more specific first) and not from F and E; in this case the MRO is GEF without any doubt.