如何在 Tkinter 中创建垂直菜单栏?

问题描述

我正在尝试制作一个垂直菜单栏,但我不知道如何制作,基本上是一个普通的菜单栏,但只是在 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...