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.


猜你喜欢

转载自blog.csdn.net/sinat_27382047/article/details/80188897