0

I have a list of menu to display on the tkinter window but how do I display the menu in such a way that it will display left then right then go to the next row then left then right again.

Right now, I am using place method to increase the y axis so it will go down by 50 for every label. But have no idea what to do with x axis to make it go right.

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

The result does go down by 50 in the y axis but alternating left and right is not possible for me. For example:

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

Also, is it possible to have a scrollbar in this menu so that it is scroll-able when it exceed the window geometry?

Thank you for your help!

Barnacle Own
  • 85
  • 1
  • 9

1 Answers1

0

I have used the following code to solve my problem but was thinking if there's a shorter way.

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()
Barnacle Own
  • 85
  • 1
  • 9