การใช้งาน Python GUI (Tkinter) : Event-Driven Programming

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: การใช้งาน Python GUI (Tkinter) : Event-Driven Programming

การใช้งาน Python GUI (Tkinter) : Event-Driven Programming

โดย Jom07 » 07/02/2018 5:11 pm

Event-Driven Programming
เป็นการสร้าง 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

ข้างบน