【问题标题】:Error "__init__() takes from 2 to 3 positional arguments but 4 were given" when I only have the 3 arguments [closed]当我只有 3 个参数时,错误“__init__() 需要 2 到 3 个位置参数,但给出了 4 个”[关闭]
【发布时间】:2021-05-06 21:16:33
【问题描述】:

好的,所以我正在尝试制作工具提示,自从我尝试制作测试工具提示后,我尝试将其绑定到的下拉框出现错误。 我试过给它列表以外的字符串。 我对这些东西很陌生,所以我不知道这里发生了什么。

#Here I import tKinter to allow myself to create a UI
from tkinter import *
from tkinter.tix import *

#This code creates the window.
root = Tk()

#This is a list of options for your government's control over the economy of your nation
econcontrol = ["Laissez Faire", "Some Government Intervention", "State Capitalism", "Planned Economy"]
econlaw = StringVar()
econlaw.set(econcontrol[0])

#Drop down box
dropdown = OptionMenu(root, econlaw, econcontrol)
dropdown.pack()

#tooltip
tip = Balloon(root)

#bind tooltip to thing
tip.bind_widget(dropdown, balloonmsg="Hello?")


#This code makes the window stay up.
root.mainloop()

【问题讨论】:

  • edit您的问题包括调试细节。这是一个有用的question checklist
  • 这也许是为什么不使用from x import *的一个例子。
  • 正确的调用是OptionMenu(root, econlaw, *econcontrol),尽管我没有立即看到给定错误消息的来源。 OptionMenu 期望容器、变量、初始值和任何附加值都作为单独的参数。 *econcontrol 单独在这里起作用,因为econcontrol 的第一个元素也是最初选择的值。
  • 更具体地说,错误消息来自tkinter.tix.OptionMenu,而不是tkinter.OptionMenu,您正在用另一个导入语句覆盖一个导入语句。

标签: python tkinter


【解决方案1】:

您的问题在于代码from tkinter.tix import *,您应该将其替换为:

from tkinter.tix import Balloon 
# Or
import tkinter.tix # then tkinter.tix.Balloon(root)

但这不会完全解决您的所有问题,因为自 3.6 版以来,tix 未维护,因此当您尝试对其进行初始化时,工具提示会给您一个错误。您可以在这里做的是,使用Pmw 工具提示或创建您自己的工具提示。

您可以看到Pmw.Balloon here 的示例,您还可以看到如何在其下方制作自己的工具提示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2018-03-07
    • 2017-02-18
    • 2014-05-01
    相关资源
    最近更新 更多