【发布时间】:2021-02-26 06:40:33
【问题描述】:
我正在做一个 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()
我想在两个类(即 PageOne 和 PageTwo)中采用 calculate_bmi 方法,并将它们继承到两个结果页面类中并显示结果。 但是当我运行代码时,就会出现这个错误:
Traceback(最近一次调用最后一次): 文件“TN1.py”,第 92 行,在 类 ResultPageOne(Frame,PageOne): TypeError:无法创建一致的方法解析 基础框架、PageOne 的订单 (MRO)
【问题讨论】:
-
PageOne已经继承自Frame,那么为什么ResultPageOne继承Frame? -
@acw1668 感谢我尝试代码时它正在工作
-
继承不能像这样共享变量。
标签: python python-3.x oop tkinter multiple-inheritance