简体   繁体   English

使用 tkinter 中带有图像的按钮的下拉菜单

[英]Drop down menu using button with image in tkinter

I'm trying to create a dropdown menu using the button with images and I will put some functions on each button, but on the drop-down menu that I use the only button that is available to use is a check button.我正在尝试使用带有图像的按钮创建一个下拉菜单,我将在每个按钮上放置一些功能,但在下拉菜单上,我使用的唯一可用按钮是一个复选按钮。 this is the program that I'm currently using.这是我目前正在使用的程序。

from tkinter import *
root = Tk()
mbtn = Menubutton(root, text="Options", relief=RAISED)
mbtn.menu = Menu(mbtn, tearoff = 0)
mbtn["menu"] = mbtn.menu

mbtn.menu.add_checkbutton(label="Outing")
mbtn.menu.add_checkbutton(label="Sleep")
mbtn.menu.add_checkbutton(label="Tour")

mbtn.pack()
root.mainloop()

Does this solve Your issue?这能解决您的问题吗?

from tkinter import *
root = Tk()
mbtn = Menubutton(root, text="Options", relief=RAISED)
mbtn.pack()
mbtn.menu = Menu(mbtn, tearoff = 0)
mbtn["menu"] = mbtn.menu

mbtn.menu.add_checkbutton(label="Outing")
mbtn.menu.add_checkbutton(label="Sleep")
mbtn.menu.add_checkbutton(label="Tour")

root.mainloop()

instead of mbtn.pack() You can obviously also use mbtn.grid() or mbtn.place() (not suggested - .place() )而不是mbtn.pack()您显然也可以使用mbtn.grid()mbtn.place() (不建议 - .place()

@TheLizzard added: You can use root.config(menu=mbtn.menu) (instead of mbtn.pack() ) to make the menu part of the window. @TheLizzard 补充说:您可以使用root.config(menu=mbtn.menu) (而不是mbtn.pack() )使菜单成为 window 的一部分。

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

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