การใช้งาน Python GUI (Tkinter) : Tkinter widget (Anchors)การควบคุมตําแหน่งภายใน
ในบาง widget สามารถควบคุมการวางตําแหน่ง item ภายในตัวของมันเองได้ เช่น ตัวอักษร Image เป็นต้น
ซึ่งมีทั้งหมด 8 จุด โดยค่าปกติจะอยู่ที่จุดกลาง (Center) สําหรับจุดอื่นๆ ที่สามารถกําหนดวาง Widget
มีดังนี้
สัญลักษณ์
N__ตําแหน่งตรงกลางด้านบนสุดของ Frame
S__ตําแหน่งตรงกลางด้านล่างสุดของ Frame
W__ตําแหน่งตรงกลางด้านซ้ายสุดของ Frame
E__ตําแหน่งตรงกลางด้านขวาสุดของ Frame
NW_ตําแหน่งมุมบนด้านซ้ายของ Frame
NE_ตําแหน่งมุมบนด้านขวาของ Frame
SE_ตําแหน่งมุมด้านล่างขวาของ Frame
SW_ตําแหน่งมุมด้านล่างซ้ายของ Frame
Code
โค้ด: เลือกทั้งหมด
from tkinter import *
from tkinter import ttk
mainfrm = Tk()
ttk. Frame (height=80, width=200). pack()
ttk.Style().configure("enter.TButton", font=("Times", "10", "bold"), anchor="e")
ttk.Style().configure("cancel.TButton", font=("Times", "10", "italic"), anchor="w")
btnEnter = ttk.Button(mainfrm, text="Enter", style="enter.TButton").place(x=10, y=30)
btnCancel = ttk.Button(mainfrm, text="Cencle", style="cancel.TButton").place(x=100, y=30)
mainfrm.mainloop()
ผลรัน
- rr.JPG (12.41 KiB) Viewed 4887 times
อธิบายคําสั่งโปรแกรม
บรรทัด 7-8 ใช้เมธอด Style) และเมธอด Configure() ของคลาส ttk และกําหนดค่าอ้างอิง
การเรียกใช้งาน ค่ารูปแบบตัวอักษร (font) และกําหนดตําแหน่งตัวอักษรด้วยออปชั่น anchor
บรรทัด 10-11 สร้างปุ่ม Button widget และเปลี่ยนรูปแบบแสดงผลตัวอักษร โดยการเพิ่มออปชัน
style = “enter.TButton” และ style = “cancel.TButton” ตามลําดับ
บทเรียน Python
VDO Tutorial - Python
บทเรียน Python Tensorflow
บทเรียน Python GUI
[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%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/5513-tkinter-python-platform.html]GUI (Tkinter)[/url] : Tkinter widget (Anchors)การควบคุมตําแหน่งภายใน [/b]
ในบาง widget สามารถควบคุมการวางตําแหน่ง item ภายในตัวของมันเองได้ เช่น ตัวอักษร Image เป็นต้น
ซึ่งมีทั้งหมด 8 จุด โดยค่าปกติจะอยู่ที่จุดกลาง (Center) สําหรับจุดอื่นๆ ที่สามารถกําหนดวาง Widget
มีดังนี้
[b]สัญลักษณ์[/b]
N__ตําแหน่งตรงกลางด้านบนสุดของ Frame
S__ตําแหน่งตรงกลางด้านล่างสุดของ Frame
W__ตําแหน่งตรงกลางด้านซ้ายสุดของ Frame
E__ตําแหน่งตรงกลางด้านขวาสุดของ Frame
NW_ตําแหน่งมุมบนด้านซ้ายของ Frame
NE_ตําแหน่งมุมบนด้านขวาของ Frame
SE_ตําแหน่งมุมด้านล่างขวาของ Frame
SW_ตําแหน่งมุมด้านล่างซ้ายของ Frame
[b]Code[/b]
[code]
from tkinter import *
from tkinter import ttk
mainfrm = Tk()
ttk. Frame (height=80, width=200). pack()
ttk.Style().configure("enter.TButton", font=("Times", "10", "bold"), anchor="e")
ttk.Style().configure("cancel.TButton", font=("Times", "10", "italic"), anchor="w")
btnEnter = ttk.Button(mainfrm, text="Enter", style="enter.TButton").place(x=10, y=30)
btnCancel = ttk.Button(mainfrm, text="Cencle", style="cancel.TButton").place(x=100, y=30)
mainfrm.mainloop()
[/code]
ผลรัน
[attachment=0]rr.JPG[/attachment]
[b]อธิบายคําสั่งโปรแกรม[/b]
บรรทัด 7-8 ใช้เมธอด Style) และเมธอด Configure() ของคลาส ttk และกําหนดค่าอ้างอิง
การเรียกใช้งาน ค่ารูปแบบตัวอักษร (font) และกําหนดตําแหน่งตัวอักษรด้วยออปชั่น anchor
บรรทัด 10-11 สร้างปุ่ม Button widget และเปลี่ยนรูปแบบแสดงผลตัวอักษร โดยการเพิ่มออปชัน
style = “enter.TButton” และ style = “cancel.TButton” ตามลําดับ
[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://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]