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列表生成器的循环技巧分享
Mar 06 Python
Python中处理字符串的相关的len()方法的使用简介
May 19 Python
python读取文本绘制动态速度曲线
Jun 21 Python
使用Python AIML搭建聊天机器人的方法示例
Jul 09 Python
python3实现在二叉树中找出和为某一值的所有路径(推荐)
Dec 26 Python
Python 序列化和反序列化库 MarshMallow 的用法实例代码
Feb 25 Python
Django 自定义404 500等错误页面的实现
Mar 08 Python
python输出第n个默尼森数的实现示例
Mar 08 Python
Pycharm 使用 Pipenv 新建的虚拟环境(图文详解)
Apr 16 Python
python爬取”顶点小说网“《纯阳剑尊》的示例代码
Oct 16 Python
python dir函数快速掌握用法技巧
Dec 09 Python
Python中Schedule模块使用详解 周期任务神器
Apr 19 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
防止本地用户用fsockopen DDOS攻击对策
2011/11/02 PHP
php简单压缩css样式示例
2016/09/22 PHP
php实现将HTML页面转换成word并且保存的方法
2016/10/14 PHP
php-fpm添加service服务的例子
2018/04/27 PHP
PHP图像处理技术实例总结【绘图、水印、验证码、图像压缩】
2018/12/08 PHP
jQuery下通过$.browser来判断浏览器.
2011/04/05 Javascript
情人节专属 纯js脚本1k大小的3D玫瑰效果
2012/02/11 Javascript
如何让div span等元素能响应键盘事件操作指南
2012/11/13 Javascript
event.currentTarget与event.target的区别介绍
2012/12/31 Javascript
转义字符(\)对JavaScript中JSON.parse的影响概述
2013/07/17 Javascript
javascript特殊用法示例介绍
2013/11/29 Javascript
使用jQuery或者原生js实现鼠标滚动加载页面新数据
2016/03/06 Javascript
微信小程序的生命周期的详解
2017/10/19 Javascript
原生JS+HTML5实现跟随鼠标一起流动的粒子动画效果
2018/05/03 Javascript
javascript中一些奇葩的日期换算方法总结
2018/11/14 Javascript
Vue动态路由缓存不相互影响的解决办法
2019/02/19 Javascript
vue计算属性computed的使用方法示例
2019/03/13 Javascript
微信小程序仿淘宝热搜词在搜索框中轮播功能
2020/01/21 Javascript
Vue使用富文本编辑器Vue-Quill-Editor(含图片自定义上传服务、清除复制粘贴样式等)
2020/05/15 Javascript
H5 js点击按钮复制文本到粘贴板
2020/11/19 Javascript
Python struct模块解析
2014/06/12 Python
Python使用urllib2模块实现断点续传下载的方法
2015/06/17 Python
python实现中文分词FMM算法实例
2015/07/10 Python
详解Python中打乱列表顺序random.shuffle()的使用方法
2019/11/11 Python
浅析Python的命名空间与作用域
2020/11/25 Python
python爬虫中抓取指数的实例讲解
2020/12/01 Python
CSS3美化表单控件全集
2016/06/29 HTML / CSS
css3绘制天猫logo实现代码
2012/11/06 HTML / CSS
CSS3+font字体文件实现圆形半透明菜单具体步骤(图解)
2013/06/03 HTML / CSS
俄罗斯最大的在线手表商店:Bestwatch.ru
2020/01/11 全球购物
最新党员的自我评价分享
2013/11/04 职场文书
村干部承诺书
2014/03/28 职场文书
社团活动总结怎么写
2014/06/30 职场文书
大专应届毕业生求职信
2014/07/15 职场文书
修改并编译golang源码的操作步骤
2021/07/25 Golang
IIS服务器中设置HTTP重定向访问HTTPS
2022/04/29 Servers