我想在tkinter python 3.1中的事件绑定之间切换

时间:2016-03-20 19:48:17

标签: python tkinter

我尝试使用tkinter python 3.1

在事件之间切换

https://docs.python.org/3.2/library/tkinter.html     #python 3.1

from tkinter import *

def hello(event):
    print("hello -Single Click, Button-l")

def goodbye(event):                           
    print("goodbye -Single Click, Button-1") 

#if count == 1:
#NameError: name 'count' is not defined
# have to add count = 1
count = 1 # now it sets count to 1 every loop.
widget = Button(None, text='Mouse Clicks')
widget.pack()

#toggle with each click first time "hello" second time "goodbye"
if count == 1:
    widget.bind('<Button-1>', hello)
    count == 0
else:
    widget.bind('<Button-1>', goodby)
    count == 1
widget.mainloop()

如何防止每次循环时将计数设置为1,以便我可以在事件之间切换?

1 个答案:

答案 0 :(得分:0)

您需要更改回调函数中的绑定。实际上,sendata('bar')语句在评估count语句时始终为1(并且此语句仅评估一次),因此if始终成为回调

为此,您可以执行以下操作。

hello