【问题标题】:Tkinter Beginner Having problems with my error code about master and etcTkinter Beginner 我的关于 master 等的错误代码有问题
【发布时间】: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 是在哪里定义的?

标签: python oop tkinter


【解决方案1】:

可能你有问题,因为你必须在LabelButton等中使用master而不是MainInterface

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

-

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

【讨论】:

  • 另外self.MainInterface应该是masterroot.mainloop需要是root.mainloop()self.config需要是master.configcreate_window需要定义,quit如果你希望它没有错误地退出。除此之外,它可以工作(c:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-06
  • 1970-01-01
相关资源
最近更新 更多