如何在tkinter中多次调用bind函数。

时间:2017-11-04 23:30:37

标签: python user-interface tkinter

每当用户输入空格时,我想在标签上打印一些东西。但是我的代码只在第一次输入空格时打印一行,而不是在此之后。

这是我的代码:

from tkinter import *
#LOOP_ACTIVE = True

def func1(self):
    lsum["text"] = "space entered"
    #root.after(0, func1)

root = Tk()


T = Text(root, height=20, width=30)
T.pack(side=RIGHT)
T.grid(row=0, column=1)
T.insert(END, "Just a text Widget\nin two lines\n")

v = IntVar()

a=Radiobutton(root, text="unigram", variable=v, value=1).grid(column=0,row=0)
b=Radiobutton(root, text="bigram", variable=v, value=2).grid(column=0,row=1)
c=Radiobutton(root, text="trigram", variable=v, value=2).grid(column=0,row=2)


T.bind("<space>",func1)

lsum = Label(root)
lsum.grid(row=0, column=2, sticky=W, pady=4)





root.mainloop()

请帮忙!

1 个答案:

答案 0 :(得分:1)

刚添加了一个计数器,让您看到代码正常工作

from tkinter import *
#LOOP_ACTIVE = True

count = 1
def func1(self):
    global count
    count += 1
    lsum["text"] = "space entered" + str(count)
    #root.after(0, func1)

root = Tk()


T = Text(root, height=20, width=30)
T.pack(side=RIGHT)
T.grid(row=0, column=1)
T.insert(END, "Just a text Widget\nin two lines\n")

v = IntVar()

a=Radiobutton(root, text="unigram", variable=v, value=1).grid(column=0,row=0)
b=Radiobutton(root, text="bigram", variable=v, value=2).grid(column=0,row=1)
c=Radiobutton(root, text="trigram", variable=v, value=2).grid(column=0,row=2)


T.bind("<space>",func1)

lsum = Label(root)
lsum.grid(row=0, column=2, sticky=W, pady=4)

root.mainloop()