【问题标题】:How to display labels in alternate left and right position?如何在左右交替位置显示标签?
【发布时间】:2019-10-18 02:32:19
【问题描述】:

我有一个要在 tkinter 窗口上显示的菜单列表,但是我如何以这样的方式显示菜单,它会先显示左然后右,然后转到下一行,然后再左再右。

现在,我正在使用 place 方法来增加 y 轴,因此每个标签都会下降 50。但不知道如何处理 x 轴以使其正确。

from tkinter import *
root = Tk()
root.geometry("800x500")

mac_meal = {"McChicken": "$7.50", ..... }

i = 100
for key in mac_meal:
  meal = key
  Label(root, text=meal, font=("times", 12, "bold"), width=20).place(x=50,y=i)
  i += 50

结果在 y 轴上确实下降了 50,但我无法左右交替。例如:

McChicken (1st in the dictionary)              McSpicy (2nd in the dictionary)
CheeseBurger (3rd....)                         Nugget (4th..)

此外,是否可以在此菜单中设置滚动条,以便在超出窗口几何形状时可以滚动?

感谢您的帮助!

【问题讨论】:

    标签: python-3.x tkinter


    【解决方案1】:

    我使用以下代码解决了我的问题,但正在考虑是否有更短的方法。

    from tkinter import *
    
    root = Tk()
    root.geometry("800x500")
    
    mac_meal = {"McChicken":"$2.70", "McSpicy": "$2.70","CheeseBurger": "$2.80","Nugget": "$2","Coke":"$1","McFlurry":"$2"}
    
    y = 50
    i = 0
    for key in mac_meal:
        x = 50
        meal = key
        if i % 2 == 1:
            x += 400    # If it is odd number, the label will be place at the right with the same y-axis
            Label(root, text=meal, font=("times", 12, "bold"), width=20).place(x=x, y=y)
        else:
            y += 50     # If it is even number, the label will be place +50 pixels below.
            Label(root, text=meal, font=("times", 12, "bold"), width=20).place(x=x, y=y)
        i += 1
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2017-02-17
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      • 2016-07-23
      • 2018-02-01
      相关资源
      最近更新 更多