Python作业。创建一个文本框和一个按钮,当文本框失去焦点、在文本框中按回车键或单击按时显示文本框的内容。

"""
创建一个文本框和一个按钮,
当文本框失去焦点、在文本框中按回车键或单击按时显示文本框的内容。
"""
from tkinter import *

win = Tk()  # 窗口
win.title('第四题')
win.geometry('210x60')  # 窗口大小
t = Text(win)

e = StringVar()

entry = Entry(t, textvariable=e)


def FocusIn(event):
    e.set('焦点')  # 在Entry中写入


def FocusOut(event):
    e.set('失去焦点')  # 在Entry中写入


def Enter(event):
    if event.char == '\r':  # \r为回车
        e.set('您按下回车')


def singleClick(event):
    print(event.num, type(event.num))
    if event.num == 1:
        e.set('您单击鼠标左键')
    elif event.num == 2:
        e.set('您单击鼠标中键')
    elif event.num == 3:
        e.set('您单击鼠标右键')
    else:
        e.set('您单击鼠标其他键')


def Button_1():
    e.set('您点击了按钮')


b = Button(win, text='这是个按钮', command=Button_1)
t.bind('<FocusIn>', FocusIn)  # 聚焦
t.bind('<FocusOut>', FocusOut)  # 不聚焦
entry.bind('<KeyPress>', Enter)  # 监视键盘
entry.bind('<Button>', singleClick)  # 监视鼠标
t.pack()  # 显示Text组件
entry.pack()  # 显示Entry
b.pack()  # 显示Button
win.mainloop()  # 显示win

仅供参考!

猜你喜欢

转载自blog.csdn.net/weixin_51767828/article/details/121391135