【发布时间】:2016-01-22 22:15:42
【问题描述】:
我的代码在没有 OOP 的情况下运行良好,
现在我正在尝试在 OOP 中实现它,但不知何故会导致一些错误。
有人可以帮忙吗?
顺便说一句,错误是:
文件“C:\Python27\lib\lib-tk\Tkinter.py”,第 2059 行,在 _setup self.tk = master.tk AttributeError: 类 MainInterface 没有属性 'tk'"
from Tkinter import *
import tkFont
class MainInterface:
def __init__(self, master):
self.master = master
# Main interface created
master.title("Dijkstra's Algorithm Solution Demonstrator")
# Title for the main interface
self.TNR24 = tkFont.Font(family="Times New Roman", size=24, weight="bold")
self.TNR28 = tkFont.Font(family="Times New Roman", size=28, weight="bold")
# Two font types that can be used later
master.overrideredirect(True)
master.geometry("{0}x{1}+0+0".format(master.winfo_screenwidth(), master.winfo_screenheight()))
# Formats the interface so that it would be in full screen
self.MainTitle = Label(MainInterface, text="Dijkstra's Algorithm Solution Demonstrator", bg="white", font=self.TNR28)
self.InterfaceMenu = Menu(MainInterface)
self.MainInterface.config(menu=self.InterfaceMenu)
self.submenu1 = Menu(self.InterfaceMenu)
self.InterfaceMenu.add_cascade(label="File", menu=self.submenu1)
self.submenu2 = Menu(self.InterfaceMenu)
self.InterfaceMenu.add_cascade(label="Help", menu=self.submenu2)
self.submenu2.add_command(label="details")
self.MainInterfaceButton1 = Button(MainInterface, text="Input New Question", font=self.TNR24, command=create_window)
self.MainInterfaceButton2 = Button(MainInterface, text="Load Up Existing Question", font=self.TNR24)
self.MainInterfaceButton3 = Button(MainInterface, text="Identify Incorrectly Applied Algorithms", font=self.TNR24)
# Three main buttons created for that interface
self.ExitButton = Button(MainInterface, text="Exit", command=self.exit_window)
# Exit button created
self.MainTitle.pack()
# Packs the main title onto the interface
self.MainInterfaceButton1.place(x=340, y=200, width=600, height=100)
self.MainInterfaceButton2.place(x=340, y=350, width=600, height=100)
self.MainInterfaceButton3.place(x=340, y=500, width=600, height=100)
self.ExitButton.place(x=1240, y=670, width=40, height=30)
# Places the buttons at desirable places
self.MainInterface.configure(background="white")
# Makes the background colour white
def exit_window(self):
self.quit()
# Function for existing the interface
def main():
root=Tk()
Lookatthis=MainInterface(root)
root.mainloop
if __name__ == "__main__":
main()
【问题讨论】:
-
是
root.mainloop只是在你的真实代码中被引用而不是实际调用,还是这是一个复制粘贴错误? -
minimal example 有什么问题?
-
显示完整的错误信息。
-
self.MainInterface是在哪里定义的?