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()

 

三、效果圖

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM