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脚本实现批量网站存活检测遇到问题及解决方法
Oct 11 Python
Python快速从注释生成文档的方法
Dec 26 Python
快速查询Python文档方法分享
Dec 27 Python
Python使用Django实现博客系统完整版
Sep 29 Python
Python+request+unittest实现接口测试框架集成实例
Mar 16 Python
PyQt5 QTableView设置某一列不可编辑的方法
Jun 25 Python
Python考拉兹猜想输出序列代码实践
Jul 05 Python
Python序列对象与String类型内置方法详解
Oct 22 Python
解决tensorflow添加ptb库的问题
Feb 10 Python
基于Python3.7.1无法导入Numpy的解决方式
Mar 09 Python
python rolling regression. 使用 Python 实现滚动回归操作
Jun 08 Python
用python监控服务器的cpu,磁盘空间,内存,超过邮件报警
Jan 29 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笔记之常用文件操作
2010/10/12 PHP
让你成为更出色的PHP开发者的10个技巧
2011/02/25 PHP
php header Content-Type类型小结
2011/07/03 PHP
php用正则表达式匹配中文实例详解
2013/11/06 PHP
php制作中间带自己定义图片二维码的方法
2014/01/27 PHP
php 解压rar文件及zip文件的方法
2014/05/05 PHP
php反序列化长度变化尾部字符串逃逸(0CTF-2016-piapiapia)
2020/02/15 PHP
MyEclipse取消验证Js的两种方法
2013/11/14 Javascript
jquery放大镜效果超漂亮噢
2013/11/15 Javascript
javascript异步编程的4种方法
2014/02/19 Javascript
jQuery之字体大小的设置方法
2014/02/27 Javascript
JavaScript前补零操作实例
2015/03/11 Javascript
Javascript中的匿名函数与封装介绍
2015/03/15 Javascript
深入理解Javascript中的自执行匿名函数
2016/06/03 Javascript
利用PM2部署node.js项目的方法教程
2017/05/10 Javascript
Vue官方推荐AJAX组件axios.js使用方法详解与API
2018/10/09 Javascript
JS模拟浏览器实现全局搜索功能
2019/09/11 Javascript
对于Python的框架中一些会话程序的管理
2015/04/20 Python
python数据结构之图深度优先和广度优先实例详解
2015/07/08 Python
python装饰器初探(推荐)
2016/07/21 Python
浅谈pandas中DataFrame关于显示值省略的解决方法
2018/04/08 Python
python实现网页录音效果
2020/10/26 Python
美国领先的机场停车聚合商:Airport Parking Reservations
2020/02/28 全球购物
体育学院毕业生自荐信
2013/11/03 职场文书
保安员岗位职责
2013/11/17 职场文书
公司中层干部的自我评价分享
2014/03/01 职场文书
婚礼主持词
2014/03/13 职场文书
亮化工程实施方案
2014/03/17 职场文书
协议书与合同的区别
2014/04/18 职场文书
安全协议书
2014/04/23 职场文书
解除同居协议书
2015/01/29 职场文书
2015高考寄语集锦
2015/02/27 职场文书
请客吃饭开场白
2015/06/01 职场文书
行政处罚事先告知书
2015/07/01 职场文书
2015年中秋晚会主持稿
2015/07/30 职场文书
MySQL复制问题的三个参数分析
2021/04/07 MySQL