【问题标题】:Python TypeError: function takes 1 positional arguments but 2 were givenPython TypeError:函数接受 1 个位置参数,但给出了 2 个
【发布时间】:2016-11-25 21:31:32
【问题描述】:

我有一个设计为像root.title(winTitle) 一样运行的函数。这是我的代码:

from tkinter import *
class UIWindow():
    def __init__(self):
        Tk()
    def setWindowTitle(winTitle):
        self.title(winTitle)

但是当我运行它时,它给出了错误:

TypeError: setWindowTitle() takes one positional argument but two was given 

我该如何解决这个问题?

【问题讨论】:

  • 您在方法中缺少 self ,因此它将是 def setWindowTitle(self, winTitle)
  • 请不要更改问题以使代码正常工作。当你这样做时,这个问题不再适用。如果您认为该问题对未来的读者有帮助,请在答案部分发布答案。如果没有,请删除问题。
  • @WozzyCoder 我添加了自我,但它似乎不起作用
  • “似乎不起作用”不是诊断性的。请显示完整的回溯。
  • 你的第二个问题是你没有正确继承Tk。将class UIWindow(): 更改为class UIWindow(Tk): 将Tk() 更改为Tk.__init__(self)

标签: python tkinter arguments typeerror


【解决方案1】:
from Tkinter import *
class UIWindow():
    def __init__(self, *arg, **kwarg):
        self.root=Tk(*arg, **kwarg)
    def setWindowTitle(self, winTitle):
        self.root.title(winTitle)

x = UIWindow()
x.setWindowTitle("This is the Test Title.")
x.root.mainloop()

你缺少自我。这是显示带有标题的窗口的小例子。

【讨论】:

  • 更新代码,给你一个示例代码。这演示设置标题。随时欢迎查询:)
  • 有效!!太感谢了。但是为什么会这样。可以给我看看吗?
猜你喜欢
  • 2020-12-16
  • 2017-07-23
  • 2021-10-11
  • 1970-01-01
  • 2017-01-12
  • 2017-04-22
  • 2019-07-07
  • 2020-12-29
  • 2021-09-13
相关资源
最近更新 更多