python ttk.Combobox 的方法
  CBxWBYsQ2HbY 2023年11月02日 46 0


ttk里的Combobox没有currentText()这种方法,它是QT里的!

也没有https://zhidao.baidu.com/question/873512818298438172.html这么误人子弟


参考http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-Combobox.html(这是手册!)




下面给出基本用法:

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, plus all the methods on the Tkinter widget described in Section 10, “The Entry, 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.


  • 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.



【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  KRsXEGSB49bk   2023年11月19日   30   0   0 gitgithub.net
  AeUHztwqqxTz   2023年11月13日   29   0   0 .net编译安装
  PVzDp58rtCI5   2023年11月13日   120   0   0 PostgreSQL.net
  Jl7FEIOr1yGV   2023年11月13日   24   0   0 CUDAhtml.net