เป็นการสร้าง Button 2 ปุ่ม และกำหนดค่า เมื่อเราเลือกปุ่มนั้นแล้วกดลงไปที่พื้นหน้าต่างตรงส่วนไหนก็ได้ จะขึ้นค่าที่เรากำหนดลงไป
โค้ด: เลือกทั้งหมด
from tkinter import *
root = Tk()
root.geometry("300x400")
root.title("Hello mouse world")
canvas = Canvas(root)
text = "hello"
def sayHello():
global text
text = "hello"
def sayGoodbye():
global text
text = "goodbye"
def buttonPressed(evt):
if evt.widget == canvas:
canvas.create_text(evt.x, evt.y, text=text)
hellob = Button(root, text="Hello", command=sayHello)
goodbyeb = Button(root, text="Good Bye", command=sayGoodbye)
root.bind("<Button-1>", buttonPressed)
canvas.pack()
hellob.pack()
goodbyeb.pack()
root.mainloop()
ศึกษาจาก : Exploring Python