Python 窗體(tkinter)下拉列表框(Combobox)實(shí)例
廢話不多說(shuō),看代碼吧!
import tkinter from tkinter import ttk def go(*args): #處理事件,*args表示可變參數(shù) print(comboxlist.get()) #打印選中的值 win=tkinter.Tk() #構(gòu)造窗體 comvalue=tkinter.StringVar()#窗體自帶的文本,新建一個(gè)值 comboxlist=ttk.Combobox(win,textvariable=comvalue) #初始化 comboxlist["values"]=("1","2","3","4") comboxlist.current(0) #選擇第一個(gè) comboxlist.bind("<<ComboboxSelected>>",go) #綁定事件,(下拉列表框被選中時(shí),綁定go()函數(shù)) comboxlist.pack() win.mainloop() #進(jìn)入消息循環(huán)
補(bǔ)充知識(shí):Python GUI 之 Combobox 學(xué)習(xí)
1. 序言
本章介紹tkinter.ttk的Combobox控件。
2. 環(huán)境信息
********************************
本系列運(yùn)行平臺(tái):Windows10 64bit
Python 版本:3.7
********************************
3. Combobox
Combobox為下拉列表控件,它可以包含一個(gè)或多個(gè)文本項(xiàng)(text item),可以設(shè)置為單選或多選。使用方式為ttk.Combobox(root,option...)。
常用的參數(shù)列表如下:
參數(shù) |
描述 |
master |
代表了父窗口 |
height |
設(shè)置顯示高度、如果未設(shè)置此項(xiàng),其大小以適應(yīng)內(nèi)容標(biāo)簽 |
width |
設(shè)置顯示寬度,如果未設(shè)置此項(xiàng),其大小以適應(yīng)內(nèi)容標(biāo)簽 |
state |
可讀狀態(tài),如state= “readonly” |
textvariable |
設(shè)置textvariable屬性 |
一些常用的函數(shù):
函數(shù) |
描述 |
get |
返回制定索引的項(xiàng)值,如listbox.get(1);返回多個(gè)項(xiàng)值,返回元組,如listbox.get(0,2);返回當(dāng)前選中項(xiàng)的索引listbox.curselection() |
values |
設(shè)定下拉列表的內(nèi)容。如 data = ["a","b","c"], cbx["values"] = data |
current(i) |
指定下拉列表生成時(shí)顯示在列表值,i = index。如current(2),顯示列表中的第三個(gè)值 |
事件:
下拉列表沒(méi)有command函數(shù)(方法)。
下拉列表的虛擬事件是 "<<ComboboxSelected>>"。
4. 實(shí)例
實(shí)例1
from tkinter import * from tkinter import ttk #Create an instance win = Tk() win.title("Learn Combobox") #create a Label lb1 = Label(win, text = "Below is a combobox 1", font = "tahoma 12 normal") lb1.grid(column = 0, row = 0, padx = 8, pady = 4) def show_select_1(): print("post_command: show_select") print(value.get()) #Define tkinter data type data = ["a","b","c"] value = StringVar() #Create a combobox, and tighter it to value cbx_1 = ttk.Combobox(win, width = 12, height = 8, textvariable = value, postcommand = show_select_1) cbx_1.grid(column = 0, row = 1) #add data to combobox cbx_1["values"] = data #====================================================================================================== #create a Label lb2 = Label(win, text = "Below is a combobox 2", font = "tahoma 12 normal") lb2.grid(column = 0, row = 4, padx = 8, pady = 4) def show_data_2(*args): print("Event: ComboboxSelected") print(cbx_2.get()) #Define tkinter data type data2 = ["a2","b2","c2","d2","e2"] #Create a combobox, and tighter it to value cbx_2 = ttk.Combobox(win, width = 12, height = 8) cbx_2.grid(column = 0, row = 5) #set cbx_2 as readonly cbx_2.configure(state = "readonly") #add data to combobox cbx_2["values"] = data2 #set the initial data [index =2] to shows up when win generated cbx_2.current(2) #bind a event cbx_2.bind("<<ComboboxSelected>>", show_data_2) win.mainloop()
以上這篇Python 窗體(tkinter)下拉列表框(Combobox)實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Django實(shí)現(xiàn)whoosh搜索引擎使用jieba分詞
這篇文章主要介紹了Django實(shí)現(xiàn)whoosh搜索引擎使用jieba分詞,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Pandas中DataFrame對(duì)象轉(zhuǎn)置(交換行列)
本文主要介紹了Pandas中DataFrame對(duì)象轉(zhuǎn)置(交換行列),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Python實(shí)現(xiàn)對(duì)百度云的文件上傳(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇Python實(shí)現(xiàn)對(duì)百度云的文件上傳(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10Python datetime模塊高效處理日期和時(shí)間的方法
Python內(nèi)置的datetime模塊提供了強(qiáng)大的工具來(lái)處理這些需求,本文將全面介紹datetime模塊的使用方法,從基礎(chǔ)操作到高級(jí)技巧,幫助你掌握Python中的日期時(shí)間處理,感興趣的朋友一起看看吧2025-05-05python3實(shí)現(xiàn)域名查詢和whois查詢功能
本篇文章給大家分享了python3實(shí)現(xiàn)域名查詢和whois查詢功能的詳細(xì)代碼,有需要的朋友參考學(xué)習(xí)下。2018-06-06ptyhon實(shí)現(xiàn)sitemap生成示例
這篇文章主要介紹了ptyhon實(shí)現(xiàn)sitemap生成示例,需要的朋友可以參考下2014-03-03tensorflow模型的save與restore,及checkpoint中讀取變量方式
這篇文章主要介紹了tensorflow模型的save與restore,及checkpoint中讀取變量方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05