鱼C论坛

 找回密码
 立即注册
查看: 3948|回复: 13

[已解决]获取combobox下拉列表框的索引

[复制链接]
发表于 2021-5-11 11:28:24 | 显示全部楼层 |阅读模式
10鱼币
当点击下拉列表框的某一选项时,返回该选项的索引值。


毕设最后一个问题了,一直解决不了...真的很急
最佳答案
2021-5-11 11:28:25
本帖最后由 qq1151985918 于 2021-5-12 10:12 编辑
秋月明 发表于 2021-5-12 10:08
这样在绑定函数外输出comb_index的值还是None,我是需要在这个函数外用到值


不会的
  1. import tkinter
  2. from tkinter import ttk

  3. comb_index = [None]
  4. def click_comb(*arg):  
  5.     print(comb.current(), comb.get())
  6.     comb_index[0] = comb.current()

  7. def show_comb_index():
  8.     print(comb_index[0])
  9. root = tkinter.Tk()
  10. comb = ttk.Combobox(root)

  11. comb["values"] = ("AAA","BBB","CCC","DDD")
  12. comb.current(0)  
  13. comb.bind("<<ComboboxSelected>>",click_comb)  
  14. comb.pack()

  15. button = tkinter.Button(text = "打印索引值", command = show_comb_index)
  16. button.pack()

  17. root.mainloop()
复制代码

最佳答案

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-11 11:28:25 | 显示全部楼层    本楼为最佳答案   
本帖最后由 qq1151985918 于 2021-5-12 10:12 编辑
秋月明 发表于 2021-5-12 10:08
这样在绑定函数外输出comb_index的值还是None,我是需要在这个函数外用到值


不会的
  1. import tkinter
  2. from tkinter import ttk

  3. comb_index = [None]
  4. def click_comb(*arg):  
  5.     print(comb.current(), comb.get())
  6.     comb_index[0] = comb.current()

  7. def show_comb_index():
  8.     print(comb_index[0])
  9. root = tkinter.Tk()
  10. comb = ttk.Combobox(root)

  11. comb["values"] = ("AAA","BBB","CCC","DDD")
  12. comb.current(0)  
  13. comb.bind("<<ComboboxSelected>>",click_comb)  
  14. comb.pack()

  15. button = tkinter.Button(text = "打印索引值", command = show_comb_index)
  16. button.pack()

  17. root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-11 11:54:37 From FishC Mobile | 显示全部楼层
tk还是qt5
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-11 15:55:07 | 显示全部楼层
  1. import tkinter
  2. from tkinter import ttk

  3. def click_comb(*arg):  
  4.     print(comb.current(), comb.get())

  5. root = tkinter.Tk()

  6. comb = ttk.Combobox(root)
  7. comb["values"]=("AAA","BBB","CCC","DDD")
  8. comb.current(0)  
  9. comb.bind("<<ComboboxSelected>>",click_comb)  
  10. comb.pack()

  11. root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-5-11 16:29:43 | 显示全部楼层

你好,我这个方法我知道;但是这个只能在绑定函数里获取,我的疑问是如何返回获取到的当前索引的索引值。
我写的 return comb.current() ,这个方法不行...
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-5-11 16:44:03 | 显示全部楼层

tkinter
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-11 18:07:17 | 显示全部楼层
本帖最后由 qq1151985918 于 2021-5-11 18:14 编辑
秋月明 发表于 2021-5-11 16:29
你好,我这个方法我知道;但是这个只能在绑定函数里获取,我的疑问是如何返回获取到的当前索引的索引值。 ...


我没明白你的意思,你说详细一些吧,你说的返回索引值是返回到哪里?要用在什么地方?不行的话可以加个按钮不就好了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-5-12 09:54:45 | 显示全部楼层
本帖最后由 秋月明 于 2021-5-12 09:56 编辑
qq1151985918 发表于 2021-5-11 18:07
我没明白你的意思,你说详细一些吧,你说的返回索引值是返回到哪里?要用在什么地方?不行的话可以加个 ...


        #设置“朝向”列表框
        label_orient = Label(frame_prehouse,text="朝向:",font=("宋体",15))
        label_orient.grid(row=2,columnspan=2,sticky=W,padx=160,pady=20)
        Orient = StringVar()
        list_orient = ttk.Combobox(frame_prehouse, textvariable=Orient, state='readonly', width=12)
        list_orient.grid(row=2,columnspan=2,sticky=E)
        value = ['东', '东北', '东南', '北', '南', '西', '西北', '西南']
        list_orient["value"] = ( '东', '东北', '东南', '北', '南', '西', '西北', '西南')
        list_orient.current(0)
        def getorient(*args):
            print(list_orient.current())
        list_orient.bind("<<ComboboxSelected>>", getorient)

就是返回一个顺序,比如我点击了东南,我需要返回一个3;我需要的是 点击列表框里任意选项后就直接得到对应的数字,不用按钮
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-12 09:56:00 From FishC Mobile | 显示全部楼层
秋月明 发表于 2021-5-12 09:54
#设置“朝向”列表框
        label_orient = Label(frame_prehouse,text="朝向:",font=("宋体 ...

我是问你返回到哪里?返回到label或者别的地方?想怎么使用这个返回值
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-5-12 09:57:20 | 显示全部楼层
本帖最后由 秋月明 于 2021-5-12 09:58 编辑
qq1151985918 发表于 2021-5-12 09:56
我是问你返回到哪里?返回到label或者别的地方?想怎么使用这个返回值


返回一个变量,我需要用到这个变量
orient_num = int(getorient()) + 4
就是调用getorient这个绑定函数后得到这个返回值
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-12 10:04:31 | 显示全部楼层
秋月明 发表于 2021-5-12 09:57
返回一个变量,我需要用到这个变量
orient_num = int(getorient()) + 4
就是调用getorient这个绑定函 ...
  1. import tkinter
  2. from tkinter import ttk

  3. comb_index = [None]
  4. def click_comb(*arg):  
  5.     print(comb.current(), comb.get())
  6.     comb_index[0] = comb.current()
  7.    
  8. root = tkinter.Tk()
  9. comb = ttk.Combobox(root)

  10. comb["values"] = ("AAA","BBB","CCC","DDD")
  11. comb.current(0)  
  12. comb.bind("<<ComboboxSelected>>",click_comb)  
  13. comb.pack()

  14. root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-5-12 10:08:23 | 显示全部楼层
本帖最后由 秋月明 于 2021-5-12 10:10 编辑


这样在绑定函数外输出comb_index的值还是None,我是需要在这个函数外用到值

算了兄弟我还是用listbox吧,它有个curselection方法可以,谢谢你帮我解答
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-12 10:20:43 | 显示全部楼层
秋月明 发表于 2021-5-12 10:08
这样在绑定函数外输出comb_index的值还是None,我是需要在这个函数外用到值

算了兄弟我还是用listbo ...

列表是可以当做全局变量使用的,或者你可以声明一个全局变量
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-5-12 10:23:39 | 显示全部楼层

解决了!这个方法我之前想到过..当时只想着return一个值了...
谢谢你!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-24 00:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表