2016-01-22 15 views
-2

我的代碼工作正常不OOP,與師父和我等錯誤代碼Tkinter的初學者有問題

現在我想實現它在面向對象的,但不知何故導致一些錯誤。

有人可以幫忙嗎?

BTW錯誤是:

File "C:\Python27\lib\lib-tk\Tkinter.py", line 2059, in _setup self.tk = master.tk AttributeError: class MainInterface has no attribute '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() 
+1

是否只有'root.mainloop'被引用,並且沒有在真正的代碼中調用,或者這是一個複製粘貼錯誤? – TigerhawkT3

+0

什麼是[最小示例](http://stackoverflow.com/help/mcve)仍然存在問題? –

+0

顯示完整的錯誤消息。 – furas

回答

2

也許你有問題,因爲你必須使用master,而不是MainInterfaceLabelButton

... = Label(master, ...) 
... = Button(master, ...) 
... = Menu(master, ...)  

-

MainInterface是n Tkinter小部件,它不能是其他小部件的父(主)。

+2

'self.MainInterface'應該是'master','root.mainloop'需要'root.mainloop()','self.config'需要'master.config','create_window'需要定義,如果你想讓它退出而沒有錯誤,你會退出。除此之外,它的作品(c: –