【问题标题】:displaying a selection from a listbox in tkinter在 tkinter 中显示列表框中的选择
【发布时间】:2020-10-26 02:55:06
【问题描述】:

只是一个 sn-p,但我试图在列表框中显示设置的乘法因子的因子。我正在使用 FirstFactor 和 SecondFactor 显示因子列表,因此是 while 循环。但是,从选择中“获取”会在消息框中显示时间表中的最后一个因素,而不是选定的因素。有什么建议吗?

def addListItems(self):
        global firstFactor
        firstFactor = 7
        global secondFactor
        secondFactor = 1
        
        while (secondFactor < 13):
            global mult
            mult = ("%d X %d" %(firstFactor, secondFactor))
            self.listbox.insert(END,mult)
            secondFactor += 1
            

    def onClickSubmit(self):
        selection = self.listbox.curselection()
        index = int(selection[0])
        answer = (firstFactor*secondFactor)
        messagebox.showinfo(title=mult, message=answer)

【问题讨论】:

  • mylistbox.get(ANCHOR)
  • 您正在使用全局变量 firstFactorsecondFactor,它们在 while 循环之后设置为最终值(分别为 7 和 12)。您应该改为从列表框的选定项目中提取这两个值。

标签: python tkinter listbox


【解决方案1】:
selected = self.listbox.get(self.listbox.curselection())
print(selected)

这里,selected 是一个变量,它满足列表框中当前选中的项目。 “get”方法有助于捕获一些内容,而“curselection()”方法,顾名思义,就是当前选中的项目

【讨论】:

    猜你喜欢
    • 2012-04-20
    • 2011-07-11
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多