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的Django框架中的静态资源管理器django-pipeline
Apr 25 Python
python查看zip包中文件及大小的方法
Jul 09 Python
在Django框架中设置语言偏好的教程
Jul 27 Python
python算法演练_One Rule 算法(详解)
May 17 Python
Python学习小技巧之利用字典的默认行为
May 20 Python
Python md5与sha1加密算法用法分析
Jul 14 Python
Python动刷新抢12306火车票的代码(附源码)
Jan 24 Python
python实现定时压缩指定文件夹发送邮件
Dec 22 Python
Python谱减法语音降噪实例
Dec 18 Python
如何解决python多种版本冲突问题
Oct 13 Python
利用Python实现自动扫雷小脚本
Dec 17 Python
Python基础知识学习之类的继承
May 31 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
php设计模式 Bridge (桥接模式)
2011/06/26 PHP
基于php在各种web服务器的运行模式详解
2013/06/03 PHP
解析PHP跨站刷票的实现代码
2013/06/18 PHP
php输出金字塔的2种实现方法
2014/12/16 PHP
谈谈 PHP7新增功能
2015/12/16 PHP
详解PHP中的外观模式facade pattern
2018/02/05 PHP
Docker搭建自己的PHP开发环境
2018/02/24 PHP
yii2安装详细流程
2018/05/23 PHP
angularjs中的$eval方法详解
2017/04/24 Javascript
vue中如何实现变量和字符串拼接
2017/06/19 Javascript
不到200行 JavaScript 代码实现富文本编辑器的方法
2018/01/03 Javascript
解决vue中无法动态修改jqgrid组件 url地址的问题
2018/03/01 Javascript
vue better scroll 无法滚动的解决方法
2018/06/07 Javascript
Vue 动态添加路由及生成菜单的方法示例
2019/06/20 Javascript
快速对接payjq的个人微信支付接口过程解析
2019/08/15 Javascript
python操作摄像头截图实现远程监控的例子
2014/03/25 Python
python 自动重连wifi windows的方法
2018/12/18 Python
django admin后台添加导出excel功能示例代码
2019/05/15 Python
把django中admin后台界面的英文修改为中文显示的方法
2019/07/26 Python
pycharm 安装JPype的教程
2019/08/08 Python
python标识符命名规范原理解析
2020/01/10 Python
Python pathlib模块使用方法及实例解析
2020/10/05 Python
Python getsizeof()和getsize()区分详解
2020/11/20 Python
css3中检验表单的required,focus,valid和invalid样式
2014/02/21 HTML / CSS
HTML5的结构和语义(1):前言
2008/10/17 HTML / CSS
HTML5适合的情人节礼物有纪念日期功能
2021/01/25 HTML / CSS
SmartBuyGlasses台湾:名牌眼镜,名牌太阳眼镜及隐形眼镜
2017/01/04 全球购物
介绍一下write命令
2014/08/10 面试题
英语翻译系毕业生求职信
2013/09/29 职场文书
库房保管员岗位职责
2014/04/07 职场文书
秋季运动会演讲稿
2014/09/16 职场文书
镇党委书记群众路线整改措施思想汇报
2014/10/13 职场文书
文明礼仪倡议书
2015/04/28 职场文书
2016年教师寒假学习心得体会
2015/10/09 职场文书
2019年家电促销广告语集锦
2019/10/21 职场文书
话题作文之成长
2019/12/09 职场文书