by ธวัชชัย แสนหาญ » 25/02/2019 2:28 pm
การใช้งาน Python GUI (Tkinter) : การสร้าง UI เพื่อเพิ่ม-ลดค่าของเวลา
การใช้งาน
Python GUI (
Tkinter)
เป็นการสร้าง UI เพื่อเพิ่ม-ลดค่าของเวลา โดยใช้ Label แสดงเวลาพร้อมกับปุ่ม + และ - เพื่อเปลี่ยนจากเดิมไปทีละ 1
แต่ต้องไม่เกินขอบเขตที่เป็นไปได้ โดยมีการใช้รูปแบบฟังก์ชันดังนี้
รูปแบบการใช้ฟังก์ชัน
Code: Select all
def hour(op): #ชั่วโมงต้องอยู่ระหว่าง 00-23
h = int(h_var.get())
if op == '+':
h = h + 1 if h < 23 else 0
elif op == '-':
h = h - 1 if h > 0 else 23
โดยขั้นแรก ต้องสร้างตัวแปร h ขึ้นมา
ต่อไปสร้าง เงื่อนไข โดยการเช็คปุ่ม + เมื่อกด + ให้ทำ คำสั่ง if op == '+':
โดย ให้ h เพิ่มค่า ทีละ 1 โดย h จะต้องมีค่า น้อยกว่า 23 ถ้าเช็คแล้ว h ไม่น้อยกว่า 23 ให้ h = 0
ต่อไปสร้าง เงื่อนไข โดยการเช็คปุ่ม - เมื่อกด - ให้ทำ คำสั่ง if op == '-':
โดย ให้ h ]fค่า ทีละ 1 โดย h จะต้องมีค่า มากกว่า 0 ถ้าเช็คแล้ว h ไม่มากกว่า 0 ให้ h = 23
โค้ดที่ใช้รันโปรแกรม
Code: Select all
from tkinter import *
window = Tk()
window.geometry('200x180')
window.config(padx=10, pady=10)
window.option_add('*font', 'tahoma 10')
window.option_add('*Button.background', 'lightgray')
window.option_add('*Button.width', 3)
def add_grid(widget, r, c):
widget.grid(row=r, column=c, padx=10, pady=5)
add_grid(Label(text='ชั่วโมง'), 0, 0)
add_grid(Label(text='นาที'), 0,1)
add_grid(Label(text='วินาที'), 0, 2)
add_grid(Button(text='+', command=lambda:hour('+')), 1, 0)
add_grid(Button(text='+', command=lambda:minute('+')), 1, 1)
add_grid(Button(text='+', command=lambda:second('+')), 1, 2)
h_var = StringVar(value='00')
m_var = StringVar(value='00')
s_var = StringVar(value='00')
add_grid(Label(textvariable=h_var), 2, 0)
add_grid(Label(textvariable=m_var), 2,1)
add_grid(Label(textvariable=s_var), 2, 2)
add_grid(Button(text='-', command=lambda:hour('-')), 3, 0)
add_grid(Button(text='-', command=lambda:minute('-')), 3, 1)
add_grid(Button(text='-', command=lambda:second('-')), 3, 2)
def hour(op): #ชั่วโมงต้องอยู่ระหว่าง 00-23
h = int(h_var.get())
if op == '+':
h = h + 1 if h < 23 else 0
elif op == '-':
h = h - 1 if h > 0 else 23
h_var.set(format(h, '02')) #จัดรูปแบบให้เป็นเลขหลัก (ถ้ามีหลักเดียวให้เติม 0 ข้างหน้า)
def minute(op): #นาทีต้องอยู่ระหว่าง 00-59
m = int(m_var.get())
if op == '+':
m = m + 1 if m < 59 else 0
elif op == '-':
m = m - 1 if m > 0 else 59
m_var.set(format(m, '02'))
def second(op):
s = int(s_var.get())
if op == '+':
s = s + 1 if s < 59 else 0
elif op == '-':
s = s - 1 if s > 0 else 59
s_var.set(format(s, '02'))
mainloop()
ผลลัพธ์รันโปรแกรม

- 112.JPG (16.05 KiB) Viewed 841 times
สรุป
เป็นการสร้าง UI เพื่อเพิ่ม-ลดค่าของเวลา โดยใช้ Label แสดงเวลาพร้อมกับปุ่ม + และ - เพื่อเปลี่ยนจากเดิมไปทีละ 1
แต่ต้องไม่เกินขอบเขตที่เป็นไปได้
ช่องทางศึกษาเพิ่มเติม
เทคนิคการเขียน Python
บทเรียน Python
Programming - C/C++ & java & Python
บทเรียน Python GUI
บทเรียน Python Framework Flask
[b][size=150]การใช้งาน Python GUI (Tkinter) : การสร้าง UI เพื่อเพิ่ม-ลดค่าของเวลา[/size][/b]
การใช้งาน[url=https://www.mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3/2417-python-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html]Python[/url] [url=https://www.mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3/2079-gui-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html]GUI[/url] ([url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/python-gui/5513-tkinter-python-platform.html]Tkinter[/url])
เป็นการสร้าง UI เพื่อเพิ่ม-ลดค่าของเวลา โดยใช้ Label แสดงเวลาพร้อมกับปุ่ม + และ - เพื่อเปลี่ยนจากเดิมไปทีละ 1
แต่ต้องไม่เกินขอบเขตที่เป็นไปได้ โดยมีการใช้รูปแบบฟังก์ชันดังนี้
[b]รูปแบบการใช้ฟังก์ชัน[/b]
[code]
def hour(op): #ชั่วโมงต้องอยู่ระหว่าง 00-23
h = int(h_var.get())
if op == '+':
h = h + 1 if h < 23 else 0
elif op == '-':
h = h - 1 if h > 0 else 23
[/code]
โดยขั้นแรก ต้องสร้างตัวแปร h ขึ้นมา
ต่อไปสร้าง เงื่อนไข โดยการเช็คปุ่ม + เมื่อกด + ให้ทำ คำสั่ง if op == '+':
โดย ให้ h เพิ่มค่า ทีละ 1 โดย h จะต้องมีค่า น้อยกว่า 23 ถ้าเช็คแล้ว h ไม่น้อยกว่า 23 ให้ h = 0
ต่อไปสร้าง เงื่อนไข โดยการเช็คปุ่ม - เมื่อกด - ให้ทำ คำสั่ง if op == '-':
โดย ให้ h ]fค่า ทีละ 1 โดย h จะต้องมีค่า มากกว่า 0 ถ้าเช็คแล้ว h ไม่มากกว่า 0 ให้ h = 23
[b]โค้ดที่ใช้รันโปรแกรม[/b]
[code]
from tkinter import *
window = Tk()
window.geometry('200x180')
window.config(padx=10, pady=10)
window.option_add('*font', 'tahoma 10')
window.option_add('*Button.background', 'lightgray')
window.option_add('*Button.width', 3)
def add_grid(widget, r, c):
widget.grid(row=r, column=c, padx=10, pady=5)
add_grid(Label(text='ชั่วโมง'), 0, 0)
add_grid(Label(text='นาที'), 0,1)
add_grid(Label(text='วินาที'), 0, 2)
add_grid(Button(text='+', command=lambda:hour('+')), 1, 0)
add_grid(Button(text='+', command=lambda:minute('+')), 1, 1)
add_grid(Button(text='+', command=lambda:second('+')), 1, 2)
h_var = StringVar(value='00')
m_var = StringVar(value='00')
s_var = StringVar(value='00')
add_grid(Label(textvariable=h_var), 2, 0)
add_grid(Label(textvariable=m_var), 2,1)
add_grid(Label(textvariable=s_var), 2, 2)
add_grid(Button(text='-', command=lambda:hour('-')), 3, 0)
add_grid(Button(text='-', command=lambda:minute('-')), 3, 1)
add_grid(Button(text='-', command=lambda:second('-')), 3, 2)
def hour(op): #ชั่วโมงต้องอยู่ระหว่าง 00-23
h = int(h_var.get())
if op == '+':
h = h + 1 if h < 23 else 0
elif op == '-':
h = h - 1 if h > 0 else 23
h_var.set(format(h, '02')) #จัดรูปแบบให้เป็นเลขหลัก (ถ้ามีหลักเดียวให้เติม 0 ข้างหน้า)
def minute(op): #นาทีต้องอยู่ระหว่าง 00-59
m = int(m_var.get())
if op == '+':
m = m + 1 if m < 59 else 0
elif op == '-':
m = m - 1 if m > 0 else 59
m_var.set(format(m, '02'))
def second(op):
s = int(s_var.get())
if op == '+':
s = s + 1 if s < 59 else 0
elif op == '-':
s = s - 1 if s > 0 else 59
s_var.set(format(s, '02'))
mainloop()
[/code]
[b]ผลลัพธ์รันโปรแกรม[/b]
[list][list][attachment=0]112.JPG[/attachment][/list][/list]
[b]สรุป[/b]
เป็นการสร้าง UI เพื่อเพิ่ม-ลดค่าของเวลา โดยใช้ Label แสดงเวลาพร้อมกับปุ่ม + และ - เพื่อเปลี่ยนจากเดิมไปทีละ 1
แต่ต้องไม่เกินขอบเขตที่เป็นไปได้
[b]ช่องทางศึกษาเพิ่มเติม[/b] :baa:
[url=https://www.mindphp.com/developer/tips-python.html]เทคนิคการเขียน Python[/url]
[url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/83-python.html]บทเรียน Python[/url]
[url=https://www.mindphp.com/forums/viewforum.php?f=16]Programming - C/C++ & java & Python[/url]
[url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/python-gui.html]บทเรียน Python GUI[/url]
[url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/python-framework-flask.html]บทเรียน Python Framework Flask[/url]