我想在tkinter python 3.1中的事件绑定之间切换
我尝试使用tkinter python 3.1在事件之间切换我想在tkinter python 3.1中的事件绑定之间切换,python,tkinter,Python,Tkinter,我尝试使用tkinter 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'
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()
#
#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()
从tkinter导入*
def hello(事件):
打印(“您好-单击一次,按钮-l”)
def再见(活动):
打印(“再见-单击一次,按钮1”)
#如果计数=1:
#NameError:未定义名称“count”
#必须添加计数=1
count=1#现在它将每个循环的count设置为1。
widget=按钮(无,text='Mouse Clicks')
widget.pack()
#每次单击时切换第一次“你好”第二次“再见”
如果计数=1:
widget.bind(“”,你好)
计数==0
其他:
widget.bind(“”,goodby)
计数==1
widget.mainloop()
如何防止每次循环时计数设置为1,以便在事件之间切换?您需要更改回调函数内部的绑定。实际上,
if
语句求值时,count
始终为1(此语句只求值一次),因此hello
将始终是回调
为此,您可以执行以下操作
widget = Button(None, text='Mouse Clicks')
def hello(event):
print("hello -Single Click, Button-l")
widget.bind('<Button-1>', goodbye)
def goodbye(event):
print("goodbye -Single Click, Button-1")
widget.bind('<Button-1>', hello)
# Initial bind
widget.bind('<Button-1>', hello)
widget=按钮(无,text=”鼠标点击“)
def hello(事件):
打印(“您好-单击一次,按钮-l”)
widget.bind(“”,再见)
def再见(活动):
打印(“再见-单击一次,按钮1”)
widget.bind(“”,你好)
#初始绑定
widget.bind(“”,你好)
什么循环?除了mainloop
之外,您没有循环。widget.mainloop()
之前的所有代码只执行一次。不,不会。如果设置count=1,则会连续循环整个程序。如果您不正确。一个简单的print语句将向您显示代码只执行一次。这就是我实际尝试使用的程序。多亏了Suever,它现在可以工作了。