繁体   English   中英

第 26 行,在 __init__ tk.Tk().__init__(self, *args, **kwargs) 中。 类型错误:create() 参数 1 必须是 str 或 None,而不是 App

[英]line 26, in __init__ tk.Tk().__init__(self, *args, **kwargs). TypeError: create() argument 1 must be str or None, not App

这里的问题是我收到一条错误消息:第 26 行,在init tk.Tk() 中。 init (self, *args, **kwargs)。 类型错误:create() 参数 1 必须是 str 或 None,而不是 App。 在有人知道如何解决这个问题之前,我从未处理过这种错误吗?

import tkinter as tk
import tkinter.ttk as ttk
from x import Monkey, Giraffe, Elephant

Large_Font  = ("Verdana", 12)


class App(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Tk().__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.pack(side='top',fill="both",expand = True)

        container.grid_rowconfigure(0, weight =1)
        container.grid_columnconfigure(0, weight = 1)

        self.simulation = []
        for row in range(5):
            self.simulation += [Monkey(), Giraffe, Elephant()]

        tk.Frame.title('Simulator')
        self.geometry("800x800")
        self.resizable(False, False)

        self.label = ttk.Label(self, text='Sim',font=("Calibri",28,"bold"))


        self.label0 = ttk.Label(self,font=("Calibri",24,"bold"), text='Monkey')
        self.label1 = ttk.Label(self,font=("Calibri",24,"bold"), text='Giraffe')
        self.label2 = ttk.Label(self, text="Flat border", font=("Calibri",24,"bold"), relief="flat")

        self.label.pack(pady=10)
        self.label0.pack( pady=11,padx=10)
        self.label1.pack(pady=11,padx=11)
        self.label2.pack(pady=11)

        self.button = ttk.Button(self, text='Feed the Animals')
        self.button['command'] = self.button_clicked
        self.button.pack(pady=12)

    def button_clicked(self):
        showinfo(title='Information', message='Hello, Tkinter!')

# Press the green button in the gutter to run the script.
if __name__ == '__main__':

    App = App()
    App.mainloop()
    time_hours(5)

您的代码中有几个问题:

  • App应该继承自tk.Tk而不是tk.Frame
  • tk.Tk().__init__(self, *args, **kwargs)应该是tk.Tk.__init__(self, *args, **kwargs)super().__init__(*args, **kwargs)
  • tk.Frame.title('Simulator')应该是self.title('Simulator')
  • self.simulation += [Monkey(), Giraffe, Elephant()]应该是self.simulation += [Monkey(), Giraffe(), Elephant()] ,我想
  • 需要添加from tkinter.messagebox import showinfo

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM