【问题标题】:Rretrieve selected value from Listbox in Python tkinterR从Python tkinter中的列表框中检索选定的值
【发布时间】:2019-02-02 23:27:13
【问题描述】:

使用 python 3.7.2 我想从Listbox 中检索选定的条目。

我一直在使用curselection()selection_set()get() 方法,并多次重新排列它们。我也试过设置self.libo(selectmode="single")

import tkinter as tk


class Gui(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()  # ipadx=3, ipady=3
        self.createWidgets()
        self.createBindings()

    def widget_width(iterable):
        """returns a integer based on the longest entry contained in
        'iterable'. 'iterable' should be a data structure like list, dict or
        tuple."""
        length_counter = 0
        for entry in iterable:
            if len(entry) > length_counter:
                length_counter = len(entry)
        return length_counter

    def createWidgets(self):
        #   Listbox LabelFrame
        self.libo_lafr = tk.LabelFrame(root)
        self.libo_lafr.pack(side="top", padx=2, pady=2, ipadx=2, ipady=2)
        #   Listbox
        self.libo_entries = ("one", "two", "three", "four", "five", "six", "seven")
        self.libo = tk.Listbox(
            self.libo_lafr, width=Gui.widget_width(self.libo_entries),
            height=len(self.libo_entries), selectmode='browse',)
        self.libo.insert("end", *self.libo_entries)
        self.libo.pack()


        self.libo.curselection()
        print(self.libo)
        self.libo_selected = self.libo.selection_set(0)
        print(self.libo_selected)
        self.libo_selection = (self.libo.get(0))
    def createBindings(self):
        self.libo.bind("<<ListboxSelect>>", print(self.libo_selection))


root = tk.Tk()
audio_output_switcher = Gui(root)
audio_output_switcher.mainloop()

我只想将 Listbox 中的选定条目作为字符串。

self.libo.curselection() 上面的版本中打印".!labelframe.!listbox"(没有“),我已经看到解包curselection()[0] 的代码示例,我还阅读了curselection() 应该返回一个列表,但如果我尝试解包它我收到了IndexError

self.libo.selection_set(0) 打印 "None"(不带“)

self.libo.get(0) 打印“一”(不带“),self.libo.get(6) 将打印 "Seven"。所以get() 按预期工作,让我可以访问Listbox 条目的索引0-6

还值得注意的是,我只有在关闭 GUI 后才会收到这些打印消息,我看过一些 youtube 视频,其中 tkinter GUI 会在 GUI 仍然打开的情况下实时打印到控制台。

我怀疑我在 createWidgets()createBidings() 函数中的错误,在 curselection() 行之前做了一个双段。

【问题讨论】:

    标签: python python-3.x tkinter listbox unpack


    【解决方案1】:

    以下似乎对我有用:

    def createWidgets(self):
        # Listbox LabelFrame
        self.libo_lafr = tk.LabelFrame(root)
        self.libo_lafr.pack(side="top", padx=2, pady=2, ipadx=2, ipady=2)
    
        # Listbox
        self.libo_entries = ("one", "two", "three", "four", "five", "six", "seven")
        self.libo = tk.Listbox(
            self.libo_lafr, width=Gui.widget_width(self.libo_entries), height=len(self.libo_entries),
            )
        self.libo.insert(tk.END, *self.libo_entries)
        self.libo.pack()
    
        self.libo.selection_set(0)
        self.libo_selection = self.libo_entries[int(self.libo.curselection()[0])]
    
        print(self.libo_selection)
    

    curselection()返回一个整数元组,尽管在某些 tkinter 版本中这些整数表示为字符串,因此需要int。这些整数可用于索引您用于创建Listbox 的数据。即使您的选择模式不允许多个,您仍然需要提取第一个。

    self.libo.selection_set(0) 打印“无”

    这段代码:

    self.libo_selected = self.libo.selection_set(0)
    print(self.libo_selected)
    

    应该打印None,因为selection_set() 不会返回任何内容。

    【讨论】:

    • selection_set(0) 必须返回某些原因,如果我将索引从 0 更改为 1-6,我会从打印的列表中获取相应的条目。我的问题是我无法让curselection()[0] 返回当前选定条目的索引。如果我复制并运行您的代码,它仍然只返回列表的第一个条目而不是当前选择的条目。
    • print(int(self.libo.curselection()[0])) 不会从列表中解压缩选定的条目,它只会返回我放入 self.libo.selection_set() 的任何索引,因此 0 表示 0,1 表示 1,2 表示 2 等等直到 6 点,之后它会抛出一个 IndexError,因为列表只有 7 个条目。
    • @theoka,如果我将createWidgets() 函数拼接到您的程序中,则将self.libo.selection_set(0) 更改为self.libo.selection_set(4),程序将five 打印到控制台并选择five在菜单上。如果您无法重现这些步骤,那么您的环境肯定有所不同。
    • 它的行为对我来说是一样的。我只是注意到如果我执行self.libo.selection_set(0, 6) 并运行程序,所有条目都默认选中。但是,如果我用光标单击任何条目并关闭 GUI,它不会打印所选值,而只会打印为 selection_set() 设置的索引。或者在 0-6 范围内只有第一个条目。
    • 我不明白,selection_set(X)self.libo_selection = (self.libo.get(int(self.libo.curselection()[0]))) 结合使用,其中 X 代表 0-6 之间的索引总是只返回该索引而不是我用光标选择的索引。如果我完全忽略了selection_set(X),我会得到一个 IndexError。
    猜你喜欢
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多