简体   繁体   English

Python tkinter 下拉菜单

[英]Python tkinter drop down menu

I am programming a menu into my first python tkinter menu window.我正在将菜单编程到我的第一个 python tkinter 菜单窗口中。 I have done it all right (I think) but the menu doesn't appear on my tkinter window.我已经完成了(我认为)但是菜单没有出现在我的 tkinter 窗口中。

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?我可以帮忙吗?

This is quite easy.这很容易。 You have not included window.config(menu=menubar) .您尚未包含window.config(menu=menubar) You should put it before the window.mainloop()你应该把它放在window.mainloop()之前

so:所以:

window.config(menu=menubar)
window.mainloop()

you forgot to config the code to menu if you understand window.config(menu=menubar)如果您了解window.config(menu=menubar)则忘记将代码配置为菜单

and also a tip ig in this code you don't need global window if for this code im sayin以及此代码中的提示 ig 如果对于此代码,我说您不需要全局窗口

只需将代码window.config(menu=menubar)放在window.mainloop()之前

The simplest way to achieve a drop-down list within Tkinter is to use the OptionMenu widget built into tkinter.在 Tkinter 中实现下拉列表的最简单方法是使用 tkinter 中内置的 OptionMenu 小部件。

from Tkinter import *

master = Tk()

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

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

mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM