GUI系列-python相应鼠标事件

相应鼠标事件python代码

import tkinter    #导入包 
root = tkinter.Tk()     #窗体对象 
def button1Click(event):    #自定义事件处理函数
    if myButton1["background"] == "green":
         myButton1["background"] = "yellow"
    else:
        myButton1["background"] = "green" 
myButton1 = tkinter.Button(root)    #按钮对象 
myButton1["text"] = "Hello, World!"    #按钮名称属性 
myButton1["background"] = "green"    #按钮背景属性 
myButton1.pack(side = tkinter.RIGHT)    #显示组件 
myButton1.bind('', button1Click)    #绑定事件 bind方法是GUI的核心技术,两个核心参数第一个说明了事件的类型(左键单击事件),第二个参数是主体的算法是谁来解决的(eventhandle),其实就是自定义函数。
myButton2 = tkinter.Button(root)    #按钮对象 
myButton2["text"] = "Python! "    #按钮名称属性 
myButton2["background"] = "red"    #按钮背景属性 
myButton2.pack(side = tkinter.LEFT)    #显示组件 
root.mainloop()    #消息循环

最后的效果如图
GUI系列-python相应鼠标事件_第1张图片
点击绿色方块的时候会变成黄色

你可能感兴趣的:(Python,GUI系列,python,event)