我的代码在没有OOP的情况下工作正常,
现在我试图在OOP中实现它,但不知何故会导致一些错误。
有人可以帮忙吗?
BTW错误是:
文件“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()
答案 0 :(得分:2)
可能您遇到问题,因为您必须在master
,MainInterface
等中使用Label
而非Button
... = Label(master, ... )
... = Button(master, ... )
... = Menu(master, ... )
-
MainInterface
不是Tkinter小部件,它不能是其他小部件的父(主)。