Python tkinter drop down menu

1.2k views Asked by At

I am programming a menu into my first python tkinter menu window. I have done it all right (I think) but the menu doesn't appear on my tkinter window.

My code is:

from tkinter import *


def f1():
    label = Label(window, text="Wassup CHUNGUS!!!")
    label.grid(row=0, column=0, sticky=W)


global window
window = Tk()
window.title("CHUNGUS")
label2 = Label(window, text="!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
label2.grid(row=2, column=0, sticky=W)
menubar = Menu(window)
firstmenu = Menu(menubar, tearoff=0)
firstmenu.add_command(label="EXIT", command=window.destroy)
firstmenu.add_command(label="CHUNGUS", command =f1)
menubar.add_cascade(label="Menu", menu=firstmenu)
window.mainloop()

Can I have some help?

4

There are 4 answers

3
Thomas On BEST ANSWER

This is quite easy. You have not included window.config(menu=menubar). You should put it before the window.mainloop()

so:

window.config(menu=menubar)
window.mainloop()
0
Lucian Chauvin On

The simplest way to achieve a drop-down list within Tkinter is to use the OptionMenu widget built into tkinter.

from Tkinter import *

master = Tk()

variable = StringVar(master)
variable.set("one") # default value

w = OptionMenu(master, variable, "one", "two", "three")
w.pack()

mainloop()
0
Ashish kumar On

Just put the code window.config(menu=menubar) before window.mainloop()

0
Paties On

you forgot to config the code to menu if you understand window.config(menu=menubar)

and also a tip ig in this code you don't need global window if for this code im sayin