Python tkinter之ComboBox(下拉框)的使用简介
作者:南风丶轻语 时间:2021-11-10 02:40:29
1、ComboBox的基础属性
# -*- encoding=utf-8 -*-
import tkinter
from tkinter import *
from tkinter import ttk
if __name__ == '__main__':
win = tkinter.Tk() # 窗口
win.title('南风丶轻语') # 标题
screenwidth = win.winfo_screenwidth() # 屏幕宽度
screenheight = win.winfo_screenheight() # 屏幕高度
width = 600
height = 500
x = int((screenwidth - width) / 2)
y = int((screenheight - height) / 2)
win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置
value = StringVar()
value.set('CCC')
values = ['AAA', 'BBB', 'CCC', 'DDD']
combobox = ttk.Combobox(
master=win, # 父容器
height=10, # 高度,下拉显示的条目数量
width=20, # 宽度
state='readonly', # 设置状态 normal(可选可输入)、readonly(只可选)、 disabled
cursor='arrow', # 鼠标移动时样式 arrow, circle, cross, plus...
font=('', 20), # 字体
textvariable=value, # 通过StringVar设置可改变的值
values=values, # 设置下拉框的选项
)
print(combobox.keys()) # 可以查看支持的参数
combobox.pack()
win.mainloop()
2、绑定选中事件
# -*- encoding=utf-8 -*-
import tkinter
from tkinter import *
from tkinter import ttk
def choose(event):
# 选中事件
print('选中的数据:{}'.format(combobox.get()))
print('value的值:{}'.format(value.get()))
if __name__ == '__main__':
win = tkinter.Tk() # 窗口
win.title('南风丶轻语') # 标题
screenwidth = win.winfo_screenwidth() # 屏幕宽度
screenheight = win.winfo_screenheight() # 屏幕高度
width = 600
height = 500
x = int((screenwidth - width) / 2)
y = int((screenheight - height) / 2)
win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置
value = StringVar()
value.set('CCC') # 默认选中CCC==combobox.current(2)
values = ['AAA', 'BBB', 'CCC', 'DDD']
combobox = ttk.Combobox(
master=win, # 父容器
height=10, # 高度,下拉显示的条目数量
width=20, # 宽度
state='normal', # 设置状态 normal(可选可输入)、readonly(只可选)、 disabled
cursor='arrow', # 鼠标移动时样式 arrow, circle, cross, plus...
font=('', 20), # 字体
textvariable=value, # 通过StringVar设置可改变的值
values=values, # 设置下拉框的选项
)
combobox.bind('<<ComboboxSelected>>', choose)
print(combobox.keys()) # 可以查看支持的参数
combobox.pack()
win.mainloop()
来源:https://www.cnblogs.com/rainbow-tan/p/14134820.html
标签:python,tkinter,下拉框,ComboBox
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python变量及数据类型用法原理汇总
2022-12-04 11:11:41
python 限制函数执行时间,自己实现timeout的实例
2023-08-03 00:49:12
深入解析Python中的WSGI接口
2021-02-03 01:22:14
python MultipartEncoder传输zip文件实例
2022-04-20 07:22:57
python图片合成的示例
2023-11-17 17:09:36
![](https://img.aspxhome.com/file/2023/2/121442_0s.png)
Python数据可视化实现多种图例代码详解
2022-02-11 04:01:38
![](https://img.aspxhome.com/file/2023/5/103665_0s.jpg)
自然描述与自然任务
2010-01-26 15:51:00
![](https://img.aspxhome.com/file/UploadPic/20101/26/pic0-99s.jpg)
Python必备基础之闭包和装饰器知识总结
2022-05-21 22:34:30
Python中如何使用Matplotlib库绘制图形
2021-09-27 11:59:04
![](https://img.aspxhome.com/file/2023/9/97699_0s.png)
Python+OpenCV实现图像融合的原理及代码
2022-08-01 00:03:52
![](https://img.aspxhome.com/file/2023/9/101239_0s.jpg)
python3利用ctypes传入一个字符串类型的列表方法
2021-06-10 20:33:36
Python版名片管理系统
2021-08-28 18:51:22
![](https://img.aspxhome.com/file/2023/7/99557_0s.png)
Dreamweaver技巧十二招
2009-07-05 18:53:00
Python psutil模块简单使用实例
2023-08-10 21:15:37
Dreamweaver MX 2004 试用心得
2010-03-25 12:21:00
![](https://img.aspxhome.com/file/UploadPic/20071/200713110316860s.gif)
Python机器视觉之基于OpenCV的手势检测
2021-06-12 10:54:11
![](https://img.aspxhome.com/file/2023/9/103359_0s.jpg)
8个asp生成随机字符的函数
2007-08-04 10:17:00
![](https://img.aspxhome.com/file/UploadPic/20078/19/2007819193914352s.jpg)
Python使用三种方法实现PCA算法
2022-06-26 13:32:49
![](https://img.aspxhome.com/file/2023/6/112026_0s.png)
Codeigniter控制器controller继承问题实例分析
2023-11-23 04:15:31
数字人组件反写[asp组件开发实例5]
2009-06-09 13:23:00
![](https://img.aspxhome.com/file/UploadPic/20093/9/20093918286639s.gif)