Tkinter 之Combobox下拉

一、参数说明

语法作用
cv = tk.stringVar() 绑定变量
com = ttk.Combobox(root, textvariable=cv) 创建下拉框
com.pack() 放置下拉框
com["value"] = ('文本',文本') 设置下拉数据
com.current(索引) 设置默认值
demo = com.get() 变量接受值
com.bind("<>", 函数名) 下拉数据点击调用函数

二、代码示例

import tkinter as tk
from tkinter import ttk

window = tk.Tk()
# 设置窗口大小
winWidth = 600
winHeight = 400
# 获取屏幕分辨率
screenWidth = window.winfo_screenwidth()
screenHeight = window.winfo_screenheight()

x = int((screenWidth - winWidth) / 2)
y = int((screenHeight - winHeight) / 2)

# 设置主窗口标题
window.title("Combobox参数说明")
# 设置窗口初始位置在屏幕居中
window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))
# 设置窗口图标
window.iconbitmap("./image/icon.ico")
# 设置窗口宽高固定
window.resizable(0, 0)

"""Ttk Combobox 参数.

        STANDARD OPTIONS

            class, cursor, style, takefocus

        WIDGET-SPECIFIC OPTIONS

            exportselection, justify, height, postcommand, state,
            textvariable, values, width
        """
values = ["红", "黄", "蓝"]
var = tk.StringVar()
def com():
    print(1)
def getValue():
    print(var.get())
ttk.Combobox(window, values=values, textvariable = var, postcommand=com).pack()
tk.Button(window, text="get value", width=30, pady=5, command=getValue).pack()
window.mainloop()

 

三、效果图

 

 

 

 

 

 

 

posted @ 2019-11-04 10:57  样子2018  阅读(2620)  评论(0编辑  收藏  举报