Python Tkinter:值太多了?

时间:2014-09-21 01:44:00

标签: python exception user-interface tkinter

我做了这个错误,以便释放鼠标按钮后文本恢复正常。如果我注释掉代码以将其绑定到鼠标释放,并且我注释掉鼠标的绑定。

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
    return self.func(*args)
  File ".\gui.py", line 53, in button1Release
    self,button1["text"] = "Hello, World!"
ValueError: too many values to unpack (expected 2)

以下是来自的代码:

#gui.py

import time
import tkinter

class GUI(object):

    #GUI.create()
    #loads interface
    def create (self, parent):

        #remember the root
        self.window = parent

        #simplify tkinter variables
        left = tkinter.LEFT
        right = tkinter.RIGHT

        self.container = tkinter.Frame(parent)
        self.button1 = tkinter.Button(self.container)
        #self.button2 = tkinter.Button(self.container)

        self.button1["text"]= "Hello, World!"
        self.button1["background"]= "white"
        self.button1.bind("<Button-1>", self.button1Click)
        self.button1.bind("<ButtonRelease-1>", self.button1Release)
        #self.button2["text"]= "Hello, World2!"
        #self.button2["background"]= "white"

        self.container.pack()
        self.button1.pack(side = left)
        #self.button2.pack(side = left)

        print("Window created")

    def run (self, parent):

        #mainloop

        parent.mainloop()

        #on window close

        print("WIndows closed!")
        time.sleep(1)

    def button1Click(self, event):

        self.button1["text"] = "Click!"

    def button1Release(self, event):

        self,button1["text"] = "Hello, World!"

1 个答案:

答案 0 :(得分:3)

您编写了一个,逗号,您打算使用.点:

self,button1["text"] = "Hello, World!"

请注意selfbutton1之间的逗号。 应该

self.button1["text"] = "Hello, World!"

Python将第一个作为两个单独的目标读取,要求您提供两个值,一个用于self,另一个用于button1["text"]