1

I have the following 3 lists:

owner = ['Spain', 'United Kingdom', 'Malaysia']
phase= ['A100', 'B100', 'C100']
machine = ['LLT', 'AAF', 'GGG', 'TGF']

With above lists I'm trying to make 3 OptionMenus, this works great. However, when I try to add titles it does not look very neat.

This is my code:

window = Tk()
window.title("Running Python Script") #Create window
window.geometry('550x300') #geo of the window

def run():
    os.system('python "c:\data\FF\Desktop\PythonFolder\FF_software.py"') 

#The run button (this button runs some other software)
run_button = Button(window, text="Run your Code!", bg="blue", fg="white",command=run)
run_button.grid(column=0, row=0)

#These are the option menus
dd_owner = StringVar(window)
dd_owner.set(owner[0]) #the first value

dd_phase= StringVar(window)
dd_phase.set(phase[0]) 

dd_machine= StringVar(window)
dd_machine.set(machine[0])

w = OptionMenu(window, dd_owner, *owner)
w2 = OptionMenu(window, dd_phase, *phase)
w3 = OptionMenu(window, dd_machine, *machine)

#These are the titles
l1 = Label(window,  text='Select Owner', width=15 )  
l1.grid(row=5,column=1)

l2 = Label(window,  text='Select phase', width=15 )  
l2.grid(row=6,column=1)

l3 = Label(window,  text='Select machine', width=15 )  
l3.grid(row=7,column=1)

w.grid()
w2.grid()
w3.grid()

mainloop()

This is my outcome:

enter image description here

As you can see, it does not look very neat. The titles are above the option menus and not beside them from the left.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
TangerCity
  • 775
  • 2
  • 7
  • 13
  • Why did you not specify any grid coordinates for the menus? – mkrieger1 May 26 '21 at 07:10
  • @mkrieger1 I totally forgot that, could that solve the problem? – TangerCity May 26 '21 at 07:19
  • If you try it you will know. – mkrieger1 May 26 '21 at 07:20
  • @mkrieger1 Yes it did. Oke thank you, by the way. Is there a way to make my code shorter? Because when I have around 10+optionMenus I just keep copying above snippets of code.. – TangerCity May 26 '21 at 07:21
  • @TangerCity you can store them in a dictionary and loop iterate through key-value. Take Label text as `dict` key and your option menu values as values for `dict`. Now you can simply loop through them and cut your code short. – Art May 26 '21 at 07:40

1 Answers1

1
w.grid(row=5,column=2)
w2.grid(row=6,column=2)
w3.grid(row=7,column=2)

replace last 3 lines with this and you are done. you can change column value as you need

  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232897/discussion-between-abhishek-aggarwal-and-tangercity). – Abhishek Aggarwal May 26 '21 at 08:17