python ttk.Combobox 的方法

ttk裏的Combobox沒有currentText()這種方法,它是QT裏的!

也沒有https://zhidao.baidu.com/question/873512818298438172.html這麼誤人子弟


參考http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-Combobox.html(這是手冊!)

以及https://blog.csdn.net/u010159842/article/details/53287325


這是幾個倒是對的

https://blog.csdn.net/sofeien/article/details/49444001

https://blog.csdn.net/houyanhua1/article/details/78174066


下面給出基本用法:

import tkinter as tk
from tkinter import ttk

win = tk.Tk()
win.title("Python GUI")    # 添加標題

label1=ttk.Label(win, text="Chooes a number")
label1.grid(column=1, row=0)    # 添加一個標籤,並將其列設置爲1,行設置爲0

# button被點擊之後會被執行
def clickMe():   # 當acction被點擊時,該函數則生效【顯示當前選擇的數】

    print(numberChosen.current())#輸出下所選的索引

    if numberChosen.current()==0 :#判斷列表當前所選~~~~~~~~~~~
        label1.config(text="選了1")#【注意,上面的label1如果直接.grid會出錯】
    if numberChosen.current()==1 :
        label1.config(text="選了6")
    if numberChosen.current()==2 :
        label1.config(text="選了第"+ str(numberChosen.current()+1)+"個")

# 按鈕
action = ttk.Button(win, text="Click Me!", command=clickMe)     # 創建一個按鈕, text:顯示按鈕上面顯示的文字, command:當這個按鈕被點擊之後會調用command函數
action.grid(column=2, row=1)    # 設置其在界面中出現的位置  column代表列   row 代表行


# 創建一個下拉列表
number = tk.StringVar()
numberChosen = ttk.Combobox(win, width=12, textvariable=number)
numberChosen['values'] = (1, 6, 3)     # 設置下拉列表的值
numberChosen.grid(column=1, row=1)      # 設置其在界面中出現的位置  column代表列   row 代表行
numberChosen.current(0)    # 設置下拉列表默認顯示的值,0爲 numberChosen['values'] 的下標值


win.mainloop()      # 當調用mainloop()時,窗口才會顯示出來



clickme()函數裏是我最想說的:ttk.Combobox裏只有   .current([index]) 和 .set(value)   函數,沒有開始QT裏那些。

以下轉自上述“手冊”

Methods on a ttk.Combobox include all those described in Section 46, “Methods common to all ttk widgets”, plus all the methods on the Tkinter widget described in Section 10, “The Entry widget”, plus:

.current([index])

To select one of the elements of the values option, pass the index of that element as the argument to this method. If you do not supply an argument, the returned value is the index of the current entry text in the values list, or -1 if the current entry text is not in the values list.

.set(value)

Set the current text in the widget to value.

The states of a ttk.Combobox widget affect its operation. To interrogate or change states, see the .instate() and .state() methods in Section 46, “Methods common to all ttk widgets”.

  • If the widget is in the disabled state, no user action will change the contents.

  • If the widget is in the !disabled state and also the readonly state, the user may change the contents by using the drop-down menu, but may not directly edit the contents.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章