[英]tkinter toolbar menu/ button open drop-down menu/gif
In the toolbar I press a button and a drop-down menu with buttons opens.在工具栏中,我按下一个按钮,然后打开一个带有按钮的下拉菜单。 The images are.图片是。 gif for buttons.按钮的 gif。 I want the button in the toolbar to open a drop-down menu with buttons (image - gif).我希望工具栏中的按钮可以打开带有按钮的下拉菜单(图像 - gif)。 How can I do this?我怎样才能做到这一点?
# Image Toolbar
img1 = PhotoImage(file = r\path\"img.gif")`
# Toolbar
toolbar = Frame(root, bd=1, relief=RAISED)
toolbar.pack(side=TOP, fill=X)
btn1 = Button(toolbar, relief=FLAT, compound= LEFT, text="",image=img1, command=submenu1)
btn1.pack(side=LEFT, padx=0, pady=0)
def submenu1():
# gif icon for submenu1:
imgvar1 = PhotoImage(file=r\path\.gif)
???
To make a button with a drop-down menu I suggest you use a Menubutton
which was designed for that purpose — it's the part that stays on the screen all the time.要制作带有下拉菜单的按钮,我建议您使用专为此目的而设计的Menubutton
它是始终停留在屏幕上的部分。 After creating one you should create a Menu
widget and configure the Menubutton
to use it.创建一个之后,您应该创建一个Menu
小部件并配置Menubutton
以使用它。
Once you have a Menu
instance, you can populate it with different kinds of menu items, including ones made of images by using the Menu.insert_cascade()
method.拥有Menu
实例后,您可以使用不同类型的菜单项填充它,包括使用Menu.insert_cascade()
方法由图像制成的菜单项。 Also see this Menu item creation documentation which describes all the different kinds of menu items you can create and add to one.另请参阅此菜单项创建文档,其中描述了您可以创建和添加的所有不同类型的菜单项。
Below is some sample code illustrating what I mean.下面是一些示例代码,说明了我的意思。 For simplicity I used the same image twice — on the Menubutton
and on one of the items on the drop-down menu that is displayed when it's clicked.为简单起见,我两次使用相同的图像 — 在Menubutton
上和在单击时显示的下拉菜单中的一个项目上。
import tkinter as tk
from tkinter.constants import *
root = tk.Tk()
# Image Toolbar
img1 = tk.PhotoImage(file="8-ball.png")
# Toolbar
toolbar = tk.Frame(root, bd=1, relief=RAISED)
toolbar.pack(side=TOP, fill=X)
menubtn = tk.Menubutton(toolbar, relief=FLAT, compound=LEFT, text="", image=img1)
menubtn.pack(side=LEFT, padx=0, pady=0)
menu = tk.Menu(menubtn, tearoff=0)
menubtn.config(menu=menu)
menu.insert_command(0, label='Submit', command=lambda: print('Submit clicked'))
imgvar1 = tk.PhotoImage(file="8-ball.png")
menu.insert_cascade(1, image=imgvar1, command=lambda: print('Image pop out clicked'))
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.