โดย Jom07 » 16/03/2018 5:12 pm
การใช้งาน Python GUI (Tkinter) : Tkinter Dropdown Menu (เมนูเลือกแบบ Dropdown)
การทำงานภาษา
Python มีฟังก์ชันต่าง ๆ ให้เลือกนำมาใช้ในการใช้งานได้อย่างเหมาะสม ทำงานร่วมกับโมดูลต่าง ๆ ที่สามารถดึงมาใช้งานร่วมกันได้ เพราะ Python ไม่ยึดติดกับแพลตฟอร์ม โดยจะพูดถึง
GUI Tkinter ที่เป็น
โมดูลอินเตอร์เฟซของ Python การทำงานหลักคือ สร้างหน้าต่างหน้าโปรแกรมขึ้นมาและภายในโปรแกรมผู้ใช้งานสามารถออกแบบได้อย่างเหมาะสม โดยจะมายกตัวอย่างการทำ Dropdown Menu
Dropdown Menu เป็นการสร้างเมนูขึ้นมาที่สามารถเลือกรายการในเมนูเดียว การทำงานนี้จะช่วยให้ลดการทำงานของโปรแกรม และง่ายต่อการนำไปใช้มากขึ้นโดยจะยกตัวอย่าง เช่น
การสร้างเมนู Dropdown Menu การเลือกภาษาโปรแกรม โดยมีเมนู 4 เมนูคือ Python ,Java, C++, C เมื่อกดเลือกเมนู โปรแกรมจะเก็บค่าการเลือกเข้าไป
ตัวอย่าง
โค้ด: เลือกทั้งหมด
from Tkinter import *
from ttk import *
root = Tk()
root.title("Dropdown Menu")
mainframe = Frame(root)
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
mainframe.pack(pady=100, padx=100)
tkvar = StringVar(root)
choices = ['Python','Java', 'C++', 'C']
popupMenu = OptionMenu(mainframe, tkvar, choices[1], *choices)
Label(mainframe, text="Choose a Language").grid(row=1, column=1)
popupMenu.grid(row=2, column=1)
tkvar.set('Python')
def change_dropdown(*args):
print(tkvar.get())
tkvar.trace('w', change_dropdown)
root.mainloop()
ผลรัน
เมื่อเลือกเมนู
บทความที่เกี่ยวข้อง:
บทเรียน Python GUI
บทเรียน Python
VDO Tutorial - Python
บทเรียน Python Tensorflow
[b]การใช้งาน Python GUI (Tkinter) : Tkinter Dropdown Menu (เมนูเลือกแบบ Dropdown) [/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] มีฟังก์ชันต่าง ๆ ให้เลือกนำมาใช้ในการใช้งานได้อย่างเหมาะสม ทำงานร่วมกับโมดูลต่าง ๆ ที่สามารถดึงมาใช้งานร่วมกันได้ เพราะ Python ไม่ยึดติดกับแพลตฟอร์ม โดยจะพูดถึง [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] ที่เป็น[url=https://www.mindphp.com/forums/viewtopic.php?f=79&t=37198]โมดูล[/url]อินเตอร์เฟซของ Python การทำงานหลักคือ สร้างหน้าต่างหน้าโปรแกรมขึ้นมาและภายในโปรแกรมผู้ใช้งานสามารถออกแบบได้อย่างเหมาะสม โดยจะมายกตัวอย่างการทำ Dropdown Menu
Dropdown Menu เป็นการสร้างเมนูขึ้นมาที่สามารถเลือกรายการในเมนูเดียว การทำงานนี้จะช่วยให้ลดการทำงานของโปรแกรม และง่ายต่อการนำไปใช้มากขึ้นโดยจะยกตัวอย่าง เช่น
การสร้างเมนู Dropdown Menu การเลือกภาษาโปรแกรม โดยมีเมนู 4 เมนูคือ Python ,Java, C++, C เมื่อกดเลือกเมนู โปรแกรมจะเก็บค่าการเลือกเข้าไป
[b]ตัวอย่าง[/b]
[code]from Tkinter import *
from ttk import *
root = Tk()
root.title("Dropdown Menu")
mainframe = Frame(root)
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
mainframe.pack(pady=100, padx=100)
tkvar = StringVar(root)
choices = ['Python','Java', 'C++', 'C']
popupMenu = OptionMenu(mainframe, tkvar, choices[1], *choices)
Label(mainframe, text="Choose a Language").grid(row=1, column=1)
popupMenu.grid(row=2, column=1)
tkvar.set('Python')
def change_dropdown(*args):
print(tkvar.get())
tkvar.trace('w', change_dropdown)
root.mainloop()
[/code]
[b]ผลรัน[/b]
[img]http://snap.mindphp.com/index.php?view=2018Mar16_5aab94d9c2ac2[/img]
เมื่อเลือกเมนู
[img]http://snap.mindphp.com/index.php?view=2018Mar16_5aab94fc0b6a2[/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-php/245-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/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]