问题描述
我正在尝试制作一个垂直菜单栏,但我不知道如何制作,基本上是一个普通的菜单栏,但只是在 gui 的左侧。甚至有可能吗?
解决方法
如果您想要一个附加到框架左侧或右侧的垂直菜单栏,我认为您无法使用默认的 tkinter.Menu 对象来做到这一点,因为您无法调用 .pack()或者改变它的锚点。
您可能需要创建自己的菜单类。您需要使用 tk.Frame 来保存您的 tkinter.Button 和 tkinter.OptionMenu 小部件,您可以使用命令选项将它们绑定到它们各自的功能。我建议你扩展 tk.Frame:
class VerticalMenu(tk.Frame):
def __init__(self,parent,**kwargs):
tk.Frame.__init__(self,**kwargs)
#Do other init here to add your menu items
命令绑定示例:
def DoSomething():
print('Hello World')
frame = tkinter.Frame(parent) #Parent will probably be your master tk.Tk object
frame.pack(anchor = 'nw') #Anchors the frame to the top-left corner,use 'ne' for top-right.
button = tkinter.Button(frame,label = 'Button Text',command = DoSomething)
button.pack(anchor = 'n') #This will anchor the button to the top of the frame,giving you your verticality