Python tkinter之ComboBox(下拉框)的使用简介


Posted in Python onFebruary 05, 2021

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()

Python tkinter之ComboBox(下拉框)的使用简介

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()

Python tkinter之ComboBox(下拉框)的使用简介

以上就是Python tkinter之ComboBox(下拉框)的使用简介的详细内容,更多关于Python tkinter之ComboBox 下拉框的使用的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python 排列组合之itertools
Mar 20 Python
下载安装setuptool和pip linux安装pip    
Jan 24 Python
Django Admin实现上传图片校验功能
Mar 06 Python
Python实现购物系统(示例讲解)
Sep 13 Python
Python实现删除排序数组中重复项的两种方法示例
Jan 31 Python
python实现简单图片物体标注工具
Mar 18 Python
详解Python数据可视化编程 - 词云生成并保存(jieba+WordCloud)
Mar 26 Python
python使用参数对嵌套字典进行取值的方法
Apr 26 Python
django框架基于模板 生成 excel(xls) 文件操作示例
Jun 19 Python
利用python实现汉字转拼音的2种方法
Aug 12 Python
TensorFlow tf.nn.max_pool实现池化操作方式
Jan 04 Python
python 图像判断,清晰度(明暗),彩色与黑白实例
Jun 04 Python
python批量提取图片信息并保存的实现
Feb 05 #Python
Python的轻量级ORM框架peewee使用教程
Feb 05 #Python
pycharm 实现光标快速移动到括号外或行尾的操作
Feb 05 #Python
pycharm进入时每次都是insert模式的解决方式
Feb 05 #Python
pycharm最新激活码有效期至2100年(亲测可用)
Feb 05 #Python
python中numpy.empty()函数实例讲解
Feb 05 #Python
解决Pycharm 运行后没有输出的问题
Feb 05 #Python
You might like
967 个函式
2006/10/09 PHP
PHP字符转义相关函数小结(php下的转义字符串)
2007/04/12 PHP
PHP 小心urldecode引发的SQL注入漏洞
2011/10/27 PHP
PHP 杂谈《重构-改善既有代码的设计》之三 重新组织数据
2012/04/09 PHP
php 解析xml 的四种方法详细介绍
2016/10/26 PHP
PHP切割汉字的常用方法实例总结
2019/04/27 PHP
jquery 全局AJAX事件使用代码
2010/11/05 Javascript
nodejs实用示例 缩址还原
2010/12/28 NodeJs
详解AngularJS中的表格使用
2015/06/16 Javascript
JS获取文件大小方法小结
2015/12/08 Javascript
修改Jquery Dialog 位置的实现方法
2016/08/26 Javascript
Bootstrap布局之栅格系统学习笔记
2017/05/04 Javascript
基于jquery实现多选下拉列表
2017/08/02 jQuery
Vue项目数据动态过滤实践及实现思路
2018/09/11 Javascript
微信小程序实现拍照画布指定区域生成图片
2019/07/18 Javascript
微信小程序嵌入腾讯视频源过程详解
2019/08/08 Javascript
vue实现计步器功能
2019/11/01 Javascript
Python实现两个list对应元素相减操作示例
2017/06/09 Python
Python学生信息管理系统修改版
2018/03/13 Python
在Python中实现shuffle给列表洗牌
2018/11/08 Python
Python任务调度模块APScheduler使用
2020/04/15 Python
网站性能延迟加载图像的五种技巧(小结)
2020/08/13 HTML / CSS
墨尔本最受欢迎的复古风格品牌:Princess Highway
2018/12/21 全球购物
应届毕业生的个人自我鉴定
2013/10/24 职场文书
鲜果饮品店创业计划书
2014/01/21 职场文书
转预备党员政审材料
2014/02/06 职场文书
国际贸易专业个人鉴定
2014/02/22 职场文书
培训讲师岗位职责
2014/04/13 职场文书
励志演讲稿大全
2014/08/21 职场文书
新疆民族团结演讲稿
2014/08/27 职场文书
医生学习党的群众路线教育实践活动心得体会
2014/11/03 职场文书
投标邀请书范本
2015/02/02 职场文书
上诉状格式
2015/05/23 职场文书
85句关于理想的名言警句大全
2019/08/22 职场文书
VUE之图片Base64编码使用ElementUI组件上传
2022/04/09 Vue.js
Java获取字符串编码格式实现思路
2022/09/23 Java/Android