[python]
view plain copy'''''Tkinter教程之OptionMenu篇'''
# OptionMenu為可選菜單,與Combox功能類似。
'''''1.創(chuàng)建OptionMenu'''
# -*- coding: utf-8 -*-
from tkinter import *
root = Tk()
v = StringVar(root)
'''''2.設(shè)置OptionMenu的顯示值'''
v.set('Python')
om = OptionMenu(root, v, 'Python', 'PHP', 'CPP', 'C', 'Java', 'JavaScript', 'VBScript')
om.pack()
print(v.get())
root.mainloop()
#OptionMenu的創(chuàng)建需要兩個(gè)必要的參數(shù),與當(dāng)前值綁定的變量,通常為一StringVar類型;另一個(gè)是提供可選的內(nèi)容列表,由OptionMenu的變參數(shù)指定。
[python]
view plain copy'''''3.打印OptionMenu的值'''
# -*- coding: utf-8 -*-
from tkinter import *
root = Tk()
v = StringVar(root)
v.set('Tkinter')
def printOption(event):
print(v.get())
# 創(chuàng)建一個(gè)OptionMenu控件
om = OptionMenu(root,
v,
'Python',
'PHP',
'CPP',
'C',
'Java',
'JavaScript',
'VBScript'
)
om.bind('<Button-1>', printOption)
om.pack()
root.mainloop()
# 每次點(diǎn)擊OptionMenu程序打印出上次選中的項(xiàng)值
[python]
view plain copy'''''4.使用list作為OptionMenu的選項(xiàng)'''
# -*- coding: utf-8 -*-
from tkinter import *
root = Tk()
Lang = ['Python', 'PHP', 'CPP', 'C', 'Java', 'JavaScript', 'VBScript']
v = StringVar(root)
v.set('Tkinter')
def printOption(event):
print(v.get())
# 創(chuàng)建一個(gè)OptionMenu控件,使用了apply函數(shù)
# om = OptionMenu(*(root, v), *tuple(Lang))
om = OptionMenu(root, v, tuple(Lang))
om.bind('<Button-1>', printOption)
om.pack()
root.mainloop()