import tkinter as tk
window = tk.Tk()
window.title('Mywindow')
window.geometry('200x100')
var = tk.StringVar()
p= tk.Label(window,textvariable=var,bg='green',font=('Arial', 12),width=15, height=2)
p.pack()
on_hit = False
def hit_me():
global on_hit
if on_hit == False:
on_hit = True
var.set('You hit me!')
else:
on_hit = False
var.set('I Love Python!')
b=tk.Button(window, text='点我', width=15, height=2,command=hit_me)
b.pack()
window.mainloop()
运行如上代码,对按钮点击二次后,在文本框中显示的文字为?( )
A
You hit me!
B
I Love Python!
C
You hit me!
D
I Love Python!
使用微信搜索喵呜刷题,轻松应对考试!
答案:
B
解析:
【喵呜刷题小喵解析】:根据代码,当点击按钮时,会执行hit_me函数。在第一次点击时,on_hit为False,所以将on_hit设置为True,并将var的值设置为'You hit me!'。在第二次点击时,on_hit为True,所以将on_hit设置为False,并将var的值设置为'I Love Python!'。因此,第二次点击后,文本框中显示的文字为'I Love Python!'。所以,正确答案是B。