通过3个参数,但trackback说我通过了4个

2024-09-28 20:38:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我在这里找这个问题已经好几个小时了。从我在网上看到的情况来看,人们传递的论据比他们想象的要多,因为我能找到的所有与这个类型错误相关的帖子。由于某些原因,这个问题只在创建从Toplevel继承的类时发生。你知道吗

加固:

Tkinter回调异常

Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:\Users\Makin Bacon\workspace\stuff\MINT-master\test3.py", line 12, in fake_error
    topErrorWindow(self, message, detail)
  File "C:\Users\Makin Bacon\workspace\stuff\MINT-master\test3.py", line 17, in __init__
    tk.Toplevel.__init__(self, master, message, detail)
TypeError: __init__() takes from 1 to 3 positional arguments but 4 were given

我甚至尝试将我的参数发送到一个只打印所有参数的伪函数,而它只打印了3个参数。你知道吗

下面是我用来测试的代码,看看传递了哪些参数。你知道吗

import tkinter as tk

class MintApp(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        tk.Button(self, text="test error", command=self.fake_error).pack()

    def fake_error(self):
        message = "test"
        detail = "test detail"
        topErrorWindow(self, message, detail)

def topErrorWindow(*items):
        for item in items:
            print("TEST:  ", item)

if __name__ == "__main__":
    App = MintApp()
    App.mainloop()

结果如下:

TEST:   .
TEST:   test
TEST:   test detail

现在我不知道为什么我会得到一个.的参数self,我想这可能是问题的一部分,但我在网上找不到任何相关的问题。你知道吗

下面是我的代码,在我看来应该创建一个带有简单标签的顶级窗口。相反,我得到了上面列出的trackback错误。你知道吗

import tkinter as tk

class MintApp(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        tk.Button(self, text="test error", command=self.fake_error).pack()

    def fake_error(self):
        message = "test"
        detail = "test detail"
        topErrorWindow(self, message, detail)


class topErrorWindow(tk.Toplevel):
    def __init__(self, master, message, detail):
        tk.Toplevel.__init__(self, master, message, detail)
        tk.Label(self, text = "{}, {}".format(message, detail)).grid(row=0, column=0, sticky="nsew")        

if __name__ == "__main__":
    App = MintApp()
    App.mainloop()

Tags: intestselfmastermessage参数initdef
1条回答
网友
1楼 · 发布于 2024-09-28 20:38:07

当您这样做时:

tk.Toplevel.__init__(self, master, message, detail)

您正在向__init__传递四个参数:selfmastermessagedetail。然而,正如错误明确指出的那样,Toplevel.__init__采用一到三个参数。你知道吗

我不知道您希望tkinter Toplevel类如何处理messagedetail,但是它们没有映射到Toplevel的任何参数。你知道吗

解决方法是不要将无用的参数传递给超类构造函数,因为它们对子类有意义,但对超类没有意义:

tk.Toplevel.__init__(self, master)

相关问题 更多 >