簡體   English   中英

Python Tkinter類對象沒有屬性“ mainloop”

[英]Python Tkinter Class Object Has No Attribute 'mainloop'

我正在嘗試編寫一個基本程序來創建和管理任務。 我遇到了一個我研究過的錯誤,但找不到合適的解決方案。 我使用root = tk.Tk()並將其傳遞給類。 但是,當我使用mainloop()方法調用該類時,出現此錯誤,並且我不知道為什么得到它:

AttributeError:'MainApplication'對象沒有屬性'mainloop'

下面是我的代碼供參考:

 import tkinter as tk


class MainApplication:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.entry = tk.Entry(self.frame)
        self.entryButton = tk.Button(self.frame, text="Enter", command = 'create_entry')
        self.titlelabel = tk.Label(self.frame, text="Enter a task and manage the list")
        self.entry.grid(row=1, column=0)
        self.entryButton.grid(row=1, column=1)
        self.titlelabel.grid(row=0, column=0, columnspan=2)
        self.configure_gui()

    def configure_gui(self):
        self.master.geometry("200x600")
        self.master.title("Tasklister 8000")

    def create_entry(self):
        entry = self.entry.get()
        self.newTask = tk.Button(self.frame, text=entry, command = 'delete_entry')
        self.newTask.grid(columnspan=2)

    def delete_entry(self):
        self.newTask.destroy()


def main():
    root = tk.Tk()
    app = MainApplication(root)
    app.mainloop()

if __name__ == '__main__':
    main()

如果root是Tkinter對象,那么我是否應該不能在我創建的類MainApplication上調用mainloop()?

在此先非常感謝您對其他文獻的任何幫助甚至指導!!

如果root是Tkinter對象,那么我是否應該不能在我創建的類MainApplication上調用mainloop()?

root是tkinter對象,而app不是。 因此,您可以調用root.mainloop() ,但不能調用app.mainloop()


當然,您的應用程序一堆self.master對象( self.masterTkself.frameFrame ,等等),但這並不意味着它一個。 如果那不明顯,請考慮以下代碼:

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
pt = Point(2, 3)

您的pt 一些整數,但這並不意味着它 1。 您不希望pt.bit_length()pt + 6float(pt)起作用,對嗎?


如果您看過看起來與您的代碼相似的示例(例如,Effbot書中有很多示例),則主要區別在於使MainApplication成為tkinter.Tktkinter.Frame的子類。 如果這樣做,則app 是tkinter對象( TkFrame )。 但是你沒有。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM