【问题标题】:How to get every item inside a listbox in tkinter?如何在 tkinter 中获取列表框中的每个项目?
【发布时间】:2021-09-14 13:15:20
【问题描述】:

如何才能将列表框中的所有项目放入变量中?我只设法得到最后一项。我想将列表框的项目打印到新窗口中,但我只打印最后一项。

  get_content = listbox2.get(0, END)
            bb = Label(receipt_window, text = "Pizzeria")
            line1 = Label(receipt_window, text = "----------------------------------------------------------------")
            for con_item in get_content:
                con = (con_item.split('PHP'))
                con1 = con[0]
                con2 = con[1]
                rec_content = f'{con1:<40}costs PHP{con2:<8}'
                receipt = Label(receipt_window, text = rec_content)
            bb.pack()
            line1.pack()
            receipt.pack()

列表框包含:

结果:

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    这是因为您对 for 循环内创建的标签使用相同的变量 receipt 并在 for 循环外调用 receipt.pack(),所以在 for 循环内创建的最后一个标签将仅打包 .

    您需要在 for 循环中调用 receipt.pack()

        get_content = listbox2.get(0, END)
        bb = Label(receipt_window, text='Pizzeria')
        line1 = Label(receipt_window, text='----------------------------------------------------------------')
        bb.pack()
        line1.pack()
        for con_item in get_content:
            con1, con2 = con_item.split('PHP')
            rec_content = f'{con1:<40}costs PHP{con2:<8}'
            receipt = Label(receipt_window, text=rec_content)
            receipt.pack()
    

    请注意,标签最好使用固定宽度(等宽)字体。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-24
      • 1970-01-01
      • 2016-06-05
      • 2014-02-16
      • 2011-08-21
      相关资源
      最近更新 更多