Sergio
Sergio

Reputation: 275

OptionMenu() not showing in tkinter

I have the following code to try and show a drop-down menu in Tkinter

import tkinter as tk
from tkinter import *

Options=['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

root = tk.Tk()


name_var = tk.StringVar()
passw_var = tk.StringVar()
tries_var = tk.StringVar()
nrco_var = tk.StringVar(root)
nrcrn1_var = tk.StringVar()
nrcrn2_var = tk.StringVar()
nrcrn3_var = tk.StringVar()
nrcrn4_var = tk.StringVar()
nrcrn5_var = tk.StringVar()
nrcrn6_var = tk.StringVar()
nrcrn7_var = tk.StringVar()
nrcrn8_var = tk.StringVar()
nrcrn9_var = tk.StringVar()
nrcrn10_var = tk.StringVar()

nrco_var.set(Options[0])
w=OptionMenu(root, nrco_var, *Options)


name_label = tk.Label(root, text='Username', font=('calibre', 10, 'bold'))
name_entry = tk.Entry(root, textvariable=name_var, font=('calibre', 10, 'normal'))
passw_label = tk.Label(root, text='Password', font=('calibre', 10, 'bold'))
passw_entry = tk.Entry(root, textvariable=passw_var, font=('calibre', 10, 'normal'), show='*')
tries_label = tk.Label(root, text='Number Of Tries', font=('calibre', 10, 'bold'))
tries_entry = tk.Entry(root, textvariable=tries_var, font=('calibre', 10, 'normal'))
nrco_label = tk.Label(root, text='Number Of C To Register', font=('calibre', 10, 'bold'))
nrcrn1_label = tk.Label(root, text='CRN1', font=('calibre', 10, 'bold'))
nrcrn1_entry = tk.Entry(root, textvariable=nrcrn1_var, font=('calibre', 10, 'normal'))
nrcrn2_label = tk.Label(root, text='CRN2', font=('calibre', 10, 'bold'))
nrcrn2_entry = tk.Entry(root, textvariable=nrcrn2_var, font=('calibre', 10, 'normal'))
nrcrn3_label = tk.Label(root, text='CRN3', font=('calibre', 10, 'bold'))
nrcrn3_entry = tk.Entry(root, textvariable=nrcrn3_var, font=('calibre', 10, 'normal'))
nrcrn4_label = tk.Label(root, text='CRN4', font=('calibre', 10, 'bold'))
nrcrn4_entry = tk.Entry(root, textvariable=nrcrn4_var, font=('calibre', 10, 'normal'))
nrcrn5_label = tk.Label(root, text='CRN5', font=('calibre', 10, 'bold'))
nrcrn5_entry = tk.Entry(root, textvariable=nrcrn5_var, font=('calibre', 10, 'normal'))
nrcrn6_label = tk.Label(root, text='CRN6', font=('calibre', 10, 'bold'))
nrcrn6_entry = tk.Entry(root, textvariable=nrcrn6_var, font=('calibre', 10, 'normal'))
nrcrn7_label = tk.Label(root, text='CRN7', font=('calibre', 10, 'bold'))
nrcrn7_entry = tk.Entry(root, textvariable=nrcrn7_var, font=('calibre', 10, 'normal'))
nrcrn8_label = tk.Label(root, text='CRN8', font=('calibre', 10, 'bold'))
nrcrn8_entry = tk.Entry(root, textvariable=nrcrn8_var, font=('calibre', 10, 'normal'))
nrcrn9_label = tk.Label(root, text='CRN9', font=('calibre', 10, 'bold'))
nrcrn9_entry = tk.Entry(root, textvariable=nrcrn9_var, font=('calibre', 10, 'normal'))
nrcrn10_label = tk.Label(root, text='CRN10', font=('calibre', 10, 'bold'))
nrcrn10_entry = tk.Entry(root, textvariable=nrcrn10_var, font=('calibre', 10, 'normal'))


name_label.grid(row=0, column=0)
name_entry.grid(row=0, column=1)
passw_label.grid(row=1, column=0)
passw_entry.grid(row=1, column=1)
tries_label.grid(row=2, column=0)
tries_entry.grid(row=2, column=1)
nrco_label.grid(row=3, column=0)
nrcrn1_label.grid(row=4, column=0)
nrcrn1_entry.grid(row=4, column=1)
nrcrn2_label.grid(row=5, column=0)
nrcrn2_entry.grid(row=5, column=1)
nrcrn3_label.grid(row=0, column=2)
nrcrn3_entry.grid(row=0, column=3)
nrcrn4_label.grid(row=1, column=2)
nrcrn4_entry.grid(row=1, column=3)
nrcrn5_label.grid(row=2, column=2)
nrcrn5_entry.grid(row=2, column=3)
nrcrn6_label.grid(row=3, column=2)
nrcrn6_entry.grid(row=3, column=3)
nrcrn7_label.grid(row=4, column=2)
nrcrn7_entry.grid(row=4, column=3)
nrcrn8_label.grid(row=5, column=2)
nrcrn8_entry.grid(row=5, column=3)
nrcrn9_label.grid(row=0, column=4)
nrcrn9_entry.grid(row=0, column=5)
nrcrn10_label.grid(row=1, column=4)
nrcrn10_entry.grid(row=1, column=5)
sub_btn = tk.Button(root, text='Submit', command=automation)
sub_btn.grid(row=2, column=5)

root.mainloop()

I only get "Number Of C" with a blank space near it, I have other entries that are showing normally (others are normal entry grids, not drop-down menus)

I also considered using w.pack() but I got an error (due to already manual setting the places of other entries manually I guess):

self.tk.call( _tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack

However, as far as I have read about pack() I can't see how the menu not appearing is due to it.

any help would be appreciated.

Upvotes: 0

Views: 56

Answers (2)

norie
norie

Reputation: 9857

I added this after the code to add the nrco_label.

w.grid(row=3,column=1)

Upvotes: 1

Delrius Euphoria
Delrius Euphoria

Reputation: 15088

You have to use:

w.grid(row=3,column=1)

Inside a single parent, you cannot mix between pack() or grid(). I think the mistake was, you using w.grid() alone, without any row or column specifications.

Upvotes: 1

Related Questions