โดย Jom07 » 09/03/2018 5:07 pm
การใช้งาน Python GUI (Tkinter) : Tkinter Frame (วิตเจ็ตเฟรม)
Python คือชื่อภาษาที่ใช้ในการเขียนโปรแกรมภาษาหนึ่ง ซึ่งถูกพัฒนาขึ้นมาโดยไม่ยึดติดกับแพลตฟอร์ม สามารถรันภาษา Python
ได้ทั้งบนระบบ Unix, Linux , Windows ในภาษา Python ก็มีโมดูลฟังก์ที่เรียกเข้ามาใช้หรือเป็นองค์ประกอบร่วมกัน จะพูดถึง Tkinter Frame
Tkinter Frame เป็นโมดูลหนึ่งที่นำมาใช้เพื่อสร้างหน้าต่างขึ้นมา และวิดเจ็ตเฟรมเป็นการจัดกลุ่มในการทำงานกับวิคเจ็ตอื่น
โดยใช้พื้นที่สี่เหลี่ยมจัตุรัสในหน้าจอเพื่อจัดระเบียบรูปแบบและให้ padding ของเครื่องมือเหล่านี้ เฟรมสามารถใช้เป็นชั้นรองพื้นเพื่อใช้เครื่องมือที่มีความซับซ้อน
ตัวอย่างในการใช้งาน Tkinter และเรียก Frame เข้ามาใช้จะเป็นการสร้างปุ่ม มา 4 ปุ่ม กำหนดค่า Frame แต่ละปุ่มต่างสีกัน ผลรัน
จะแสดงตัวอักษรคนละสีกัน แต่เมื่อกดคลิกปุ่มนั้น ปุ่มจะเปลี่ยนเป็นสีดำดังเดิม จะเปลี่ยนแค่ตอนกดปุ่มเท่านั้น
ตัวอย่าง
โค้ด: เลือกทั้งหมด
from Tkinter import *
root = Tk()
canvas = Canvas(root, width = 300, height = 230)
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = LEFT )
greenbutton = Button(frame, text="Green", fg="light green", width = 8, height = 2)
greenbutton.pack( side = LEFT )
redbutton = Button(frame, text="Red", fg="red", width = 8, height = 2)
redbutton.pack( side = LEFT)
bluebutton = Button(frame, text="Blue", fg="blue", width = 8, height = 2)
bluebutton.pack( side = LEFT )
pinkbutton = Button(frame, text="Pink", fg="pink", width = 8, height = 2)
pinkbutton.pack( side = LEFT)
canvas.pack()
root.mainloop()
ผลรัน
สามารถนำไปปรับใช้กับงานต่าง ๆ ได้
บทความที่เกี่ยวข้อง
บทเรียน Python
VDO Tutorial - Python
บทเรียน Python Tensorflow
บทเรียน Python GUI
[size=110][b]การใช้งาน Python GUI (Tkinter) : Tkinter Frame (วิตเจ็ตเฟรม)[/b]
[b]Python[/b] คือชื่อภาษาที่ใช้ในการเขียนโปรแกรมภาษาหนึ่ง ซึ่งถูกพัฒนาขึ้นมาโดยไม่ยึดติดกับแพลตฟอร์ม สามารถรันภาษา Python
ได้ทั้งบนระบบ Unix, [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/2098-linux-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html]Linux[/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/2124-windows-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html]Windows[/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/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] ก็มีโมดูลฟังก์ที่เรียกเข้ามาใช้หรือเป็นองค์ประกอบร่วมกัน จะพูดถึง Tkinter Frame
[b]Tkinter Frame [/b]เป็นโมดูลหนึ่งที่นำมาใช้เพื่อสร้างหน้าต่างขึ้นมา และวิดเจ็ตเฟรมเป็นการจัดกลุ่มในการทำงานกับวิคเจ็ตอื่น
โดยใช้พื้นที่สี่เหลี่ยมจัตุรัสในหน้าจอเพื่อจัดระเบียบรูปแบบและให้ padding ของเครื่องมือเหล่านี้ เฟรมสามารถใช้เป็นชั้นรองพื้นเพื่อใช้เครื่องมือที่มีความซับซ้อน
ตัวอย่างในการใช้งาน Tkinter และเรียก Frame เข้ามาใช้จะเป็นการสร้างปุ่ม มา 4 ปุ่ม กำหนดค่า Frame แต่ละปุ่มต่างสีกัน ผลรัน
จะแสดงตัวอักษรคนละสีกัน แต่เมื่อกดคลิกปุ่มนั้น ปุ่มจะเปลี่ยนเป็นสีดำดังเดิม จะเปลี่ยนแค่ตอนกดปุ่มเท่านั้น[/size]
[b]ตัวอย่าง[/b]
[code]from Tkinter import *
root = Tk()
canvas = Canvas(root, width = 300, height = 230)
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = LEFT )
greenbutton = Button(frame, text="Green", fg="light green", width = 8, height = 2)
greenbutton.pack( side = LEFT )
redbutton = Button(frame, text="Red", fg="red", width = 8, height = 2)
redbutton.pack( side = LEFT)
bluebutton = Button(frame, text="Blue", fg="blue", width = 8, height = 2)
bluebutton.pack( side = LEFT )
pinkbutton = Button(frame, text="Pink", fg="pink", width = 8, height = 2)
pinkbutton.pack( side = LEFT)
canvas.pack()
root.mainloop()[/code]
[b]ผลรัน[/b]
[img]http://snap.mindphp.com/index.php?view=2018Mar09_5aa2552210e03[/img]
สามารถนำไปปรับใช้กับงานต่าง ๆ ได้
[b]บทความที่เกี่ยวข้อง[/b]
[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/vdo-tutorial-python.html]VDO Tutorial - 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-php/246-python-tensorflow.html]บทเรียน Python Tensorflow[/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-php/245-python-gui.html]บทเรียน Python GUI[/url]