立即学习:https://edu.csdn.net/course/play/19711/343106?utm_source=blogtoedu
1.单击事件:主要的单击事件是鼠标左键单击事件,一般是与按钮绑定在一起
2.关键代码:这里是以按钮绑定组件单击事件为例,单击的响应是弹出简单的对话框(simpledialog)
import tkinter
import tkinter.simpledialog#简单对话框模块
..................................................................................................................................................
#-----------创建按钮组件---------------
self.button = tkinter.Button(text = "点击输入信息",font =("微软雅黑",20))#创建按钮
self.button.bind("<Button-1>",lambda event:self.event_handle(event))#绑定事件
self.button.pack()#显示按钮
....................................................................................................................................................
def event_handle(self,event):
info_message = tkinter.simpledialog.askstring("简单对话框","请输入需要提示的信息:")
label_text = tkinter.Label(self.root,text = info_message,font =("微软雅黑",20))
label_text.pack()
3.完整代码
import tkinter
import tkinter.simpledialog#简单对话框模块
class Mainwindow():
def __init__(self):
self.root = tkinter.Tk()
self.root.title("linlianqin")
self.root.geometry("600x500")
#-----------创建按钮组件---------------
self.button = tkinter.Button(text = "点击输入信息",font =("微软雅黑",20))#创建按钮
self.button.bind("<Button-1>",lambda event:self.event_handle(event))#绑定事件
self.button.pack()#显示按钮
self.root.mainloop()
def event_handle(self,event):
info_message = tkinter.simpledialog.askstring("简单对话框","请输入需要提示的信息:")
label_text = tkinter.Label(self.root,text = info_message,font =("微软雅黑",20))
label_text.pack()
def main():
Mainwindow()
if __name__ == '__main__':
main()