【问题标题】:How to a convert a listbox item's name to a label in Python如何将列表框项目的名称转换为 Python 中的标签
【发布时间】:2021-09-21 15:17:03
【问题描述】:

对于我需要编写的程序,我需要 Listbox 项目的名称也是标签中的文本。这是我认为可行的代码,但它仅将标签配置为列表框中的项目编号,而不是名称:

def openAccount(self):
    selectedItem = ltboxSrcUser.curselection()
    for item in selectedItem:
        rootMainPage.withdraw()
        rootOthAccPage.deiconify()
        lblOtherUserName.config(text = ltboxSrcUser.curselection())

谁能帮帮我?

【问题讨论】:

  • 你能再分享一些代码吗?另外,selectedItem = ltboxSrcUser.ACTIVE (ltboxSrcUser.curselection())
  • 尝试使用<tkinter.Listbox>.get(<tkinter.Listbox>.curselection())

标签: python user-interface tkinter listbox label


【解决方案1】:

列表框的文档显示curselection 返回所选项目的索引,而不是值。要获取值,您必须将索引传递给 get 方法。

def openAccount(self):
    selectedItems = ltboxSrcUser.curselection()
    if selectedItems:
        rootMainPage.withdraw()
        rootOthAccPage.deiconify()
        index = selectedItems[0]
        item = ltboxSrcUser.get(index)
        lblOtherUserName.config(text=item)

【讨论】:

    猜你喜欢
    • 2017-03-25
    • 2022-01-10
    • 2012-07-21
    • 2022-11-29
    • 1970-01-01
    • 2015-05-27
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多