Python之tkinter組合框Combobox用法及說(shuō)明
組合框 Combobox 簡(jiǎn)介
Combobox 可以翻譯為組合框,這是tkinter.ttk
的 Widget控件,它的特性與OptionMenu類(lèi)似,可以說(shuō)它是Entry和下拉菜單的組合,構(gòu)造方法如下
Combobox(父對(duì)象, options)
參數(shù):
- 第一個(gè)參數(shù):子組件,表示這個(gè)組合框?qū)⒔⒃谀囊粋€(gè)窗口內(nèi)
- 第二個(gè)參數(shù):
options
,參數(shù)如下
參數(shù) | 含義 |
---|---|
textvariable | 可以設(shè)置Combobox 的變量值 |
value | Combobox 的選項(xiàng)內(nèi)容以元組方式存在 |
建立 Combobox
在 Combobox()的構(gòu)造方法中,可以使用Value 參數(shù)建立選項(xiàng)內(nèi)容
例子:
import tkinter import tkinter.ttk root = tkinter.Tk() var = tkinter.StringVar() combobox = tkinter.ttk.Combobox(root, textvariable=var, value=('python', 'java', 'C', 'C++')) combobox.pack(padx=5, pady=10) root.mainloop()
運(yùn)行結(jié)果:
若是選項(xiàng)很多,可以使用獨(dú)立在Combobox()外來(lái)處理
例子:
combobox = tkinter.ttk.Combobox(root, textvariable=var) combobox['value'] = ('python', 'java', 'C', 'C++')
運(yùn)行結(jié)果與上面相同
設(shè)置默認(rèn)選項(xiàng) current()
例子:
import tkinter import tkinter.ttk root = tkinter.Tk() var = tkinter.StringVar() combobox = tkinter.ttk.Combobox(root, textvariable=var) combobox['value'] = ('python', 'java', 'C', 'C++') combobox.current(0) combobox.pack(padx=5, pady=10) root.mainloop()
運(yùn)行結(jié)果:
或者可以使用var.set()
方法來(lái)設(shè)置,但是相比而言還是current()
比較便利
例子:
var.set(combobox['value'][0]) # combobox.current(0)
運(yùn)行結(jié)果一樣
獲得目前選項(xiàng) get()
例子:
import tkinter import tkinter.ttk def show(): # 使用var.get()來(lái)獲得目前選項(xiàng)內(nèi)容 varLabel.set(var.get()) root = tkinter.Tk() var = tkinter.StringVar() combobox = tkinter.ttk.Combobox(root, textvariable=var) combobox['value'] = ('python', 'java', 'C', 'C++') combobox.current(0) combobox.pack(padx=5, pady=10) varLabel = tkinter.StringVar() label = tkinter.Label(root, textvariable=varLabel, width=20, height=3, bg='lightblue', fg='red') label.pack() button = tkinter.Button(root, text='print', command=show) button.pack() root.mainloop()
運(yùn)行結(jié)果:
綁定 Combobox
當(dāng)Combobox 中的選項(xiàng)有變動(dòng)時(shí),會(huì)產(chǎn)生虛擬事件<<ComboboxSelected>>
,可以使用這個(gè)特性將此事件綁定處理方法
例子:
import tkinter import tkinter.ttk def show(event): # 使用var.get()來(lái)獲得目前選項(xiàng)內(nèi)容 varLabel.set(var.get()) root = tkinter.Tk() var = tkinter.StringVar() combobox = tkinter.ttk.Combobox(root, textvariable=var) combobox['value'] = ('python', 'java', 'C', 'C++') combobox.current(0) combobox.bind('<<ComboboxSelected>>', show) combobox.pack(side=tkinter.LEFT, padx=5, pady=10) varLabel = tkinter.StringVar() label = tkinter.Label(root, textvariable=varLabel, width=6, bg='lightblue', fg='red') label.pack(side=tkinter.LEFT) root.mainloop()
運(yùn)行結(jié)果:
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python Lambda函數(shù)使用總結(jié)詳解
這篇文章主要介紹了Python Lambda函數(shù)使用總結(jié)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12python3 flask實(shí)現(xiàn)文件上傳功能
這篇文章主要為大家詳細(xì)介紹了python3 flask實(shí)現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Anaconda 查看、創(chuàng)建、管理和使用python環(huán)境的方法
這篇文章主要介紹了Anaconda 查看、創(chuàng)建、管理和使用python環(huán)境的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12使用python實(shí)現(xiàn)微信小程序自動(dòng)簽到功能
這篇文章主要介紹了使用python實(shí)現(xiàn)微信小程序自動(dòng)簽到功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04python神經(jīng)網(wǎng)絡(luò)Keras搭建RFBnet目標(biāo)檢測(cè)平臺(tái)
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)Keras搭建RFBnet目標(biāo)檢測(cè)平臺(tái),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Python接口測(cè)試結(jié)果集實(shí)現(xiàn)封裝比較
這篇文章主要介紹了Python接口測(cè)試結(jié)果集比較封裝,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05python實(shí)現(xiàn)登錄與注冊(cè)功能
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)登錄與注冊(cè)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Numpy ndarray 多維數(shù)組對(duì)象的使用
這篇文章主要介紹了Numpy ndarray 多維數(shù)組對(duì)象的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02