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和libnmapd,提取Nmap扫描结果
Aug 23 Python
Python 出现错误TypeError: ‘NoneType’ object is not iterable解决办法
Jan 12 Python
python实现寻找最长回文子序列的方法
Jun 02 Python
Python同步遍历多个列表的示例
Feb 19 Python
python3 中的字符串(单引号、双引号、三引号)以及字符串与数字的运算
Jul 18 Python
django foreignkey(外键)的实现
Jul 29 Python
python之列表推导式的用法
Nov 29 Python
利用Python实现某OA系统的自动定位功能
May 27 Python
Python Matplotlib简易教程(小白教程)
Jul 28 Python
使用Djongo模块在Django中使用MongoDB数据库
Jun 20 Python
opencv用VS2013调试时用Image Watch插件查看图片
Jul 26 Python
Pygame Rect区域位置的使用(图文)
Nov 17 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
linux实现php定时执行cron任务详解
2013/12/24 PHP
php数组生成html下拉列表的方法
2015/07/20 PHP
Yii实现微信公众号场景二维码的方法实例
2020/08/30 PHP
Javascript 个人笔记(没有整理,很乱)
2007/07/07 Javascript
Ext.MessageBox工具类简介
2009/12/10 Javascript
jquery入门—数据删除与隔行变色以及图片预览
2013/01/07 Javascript
JavaScript生成GUID的多种算法小结
2013/08/18 Javascript
js和html5实现手机端刮刮卡抽奖效果完美兼容android/IOS
2013/11/18 Javascript
键盘KeyCode值列表汇总
2013/11/26 Javascript
jQuery中:last-child选择器用法实例
2014/12/31 Javascript
简单纯js实现点击切换TAB标签实例
2015/08/23 Javascript
分享javascript实现的冒泡排序代码并优化
2016/06/05 Javascript
JS获取IE版本号与HTML设置IE文档模式的方法
2016/10/09 Javascript
js时间戳和c#时间戳互转方法(推荐)
2017/02/15 Javascript
解决vue移动端适配问题
2018/12/12 Javascript
用node开发并发布一个cli工具的方法步骤
2019/01/03 Javascript
这样回答继承可能面试官更满意
2019/12/10 Javascript
Bootstrap简单实用的表单验证插件BootstrapValidator用法实例详解
2020/03/29 Javascript
vue-video-player实现实时视频播放方式(监控设备-rtmp流)
2020/08/10 Javascript
Python输出带颜色的字符串实例
2017/10/10 Python
Python安装模块的常见问题及解决方法
2018/02/05 Python
Python os.access()用法实例
2019/02/18 Python
Python_查看sqlite3表结构,查询语句的示例代码
2019/07/17 Python
python将字符串list写入excel和txt的实例
2019/07/20 Python
使用pymysql查询数据库,把结果保存为列表并获取指定元素下标实例
2020/05/15 Python
基于Python实现体育彩票选号器功能代码实例
2020/09/16 Python
HTML5 CSS3实现一个精美VCD包装盒个性幻灯片案例
2014/06/16 HTML / CSS
THE OUTNET美国官网:国际设计师品牌折扣网站
2017/03/07 全球购物
美国著名手表网站:Timepiece
2017/11/15 全球购物
求职信模板怎么做
2014/01/26 职场文书
小学社会实践活动总结
2014/07/03 职场文书
租房协议书样本
2014/08/20 职场文书
2014领导班子正风肃纪思想汇报
2014/09/18 职场文书
2014年团总支工作总结
2014/11/21 职场文书
特此通知格式
2015/04/27 职场文书
2015年学校教研室主任工作总结
2015/07/20 职场文书