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实现bitmap数据结构详解
Feb 17 Python
Python中用Decorator来简化元编程的教程
Apr 13 Python
Python找出文件中使用率最高的汉字实例详解
Jun 03 Python
python开发中range()函数用法实例分析
Nov 12 Python
Python数据类型学习笔记
Jan 13 Python
Python 搭建Web站点之Web服务器与Web框架
Nov 06 Python
Python3一行代码实现图片文字识别的示例
Jan 15 Python
pycharm远程linux开发和调试代码的方法
Jul 17 Python
python 爬取学信网登录页面的例子
Aug 13 Python
Python小程序之在图片上加入数字的代码
Nov 26 Python
Python代码需要缩进吗
Jul 01 Python
Python获取指定日期是"星期几"的6种方法
Mar 13 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自动更新新闻DIY
2006/10/09 PHP
PHP静态文件生成类实例
2014/11/29 PHP
php实现可运算的验证码
2015/11/10 PHP
PHP编程获取图片的主色调的方法【基于Imagick扩展】
2017/08/02 PHP
PHP实现UTF8二进制及明文字符串的转化功能示例
2017/11/20 PHP
PHP从零开始打造自己的MVC框架之入口文件实现方法详解
2019/06/03 PHP
让广告代码不再影响你的网页加载速度
2006/07/07 Javascript
jquery实现marquee效果(文字或者图片的水平垂直滚动)
2013/01/07 Javascript
js转义字符介绍
2013/11/05 Javascript
文本域中换行符的替换示例
2014/03/04 Javascript
JS+CSS实现感应鼠标渐变显示DIV层的方法
2015/02/20 Javascript
Jquery简单分页实现方法
2015/07/24 Javascript
浅谈javascript的Touch事件
2015/09/27 Javascript
JavaScript中获取时间的函数集
2016/08/16 Javascript
使用Node.js实现RESTful API的示例
2017/08/01 Javascript
vuejs2.0运用原生js实现简单拖拽元素功能
2020/08/21 Javascript
vue-cli脚手架打包静态资源请求出错的原因与解决
2019/06/06 Javascript
微信小程序实现禁止分享代码实例
2019/10/19 Javascript
vue实现简单图片上传
2020/06/30 Javascript
vue v-for 点击当前行,获取当前行数据及event当前事件对象的操作
2020/09/10 Javascript
编写Python CGI脚本的教程
2015/06/29 Python
将python图片转为二进制文本的实例
2019/01/24 Python
python查找重复图片并删除(图片去重)
2019/07/16 Python
python如何实现从视频中提取每秒图片
2020/10/22 Python
对python3中的RE(正则表达式)-详细总结
2019/07/23 Python
Python 3 判断2个字典相同
2019/08/06 Python
pip install 使用国内镜像的方法示例
2020/04/03 Python
联想德国官网:Lenovo Germany
2018/07/04 全球购物
应聘美工求职信
2013/11/07 职场文书
元旦寄语大全
2014/04/10 职场文书
教师批评与自我批评剖析材料
2014/10/16 职场文书
中学生检讨书1000字
2014/10/28 职场文书
2014年保管员工作总结
2014/11/18 职场文书
小学教师先进事迹材料
2014/12/15 职场文书
优秀共产党员推荐材料
2014/12/18 职场文书
2015年采购部工作总结
2015/04/23 职场文书