【问题标题】:Tkinter option cursor in Menu class菜单类中的 Tkinter 选项光标
【发布时间】:2019-01-10 08:53:00
【问题描述】:

我试图在鼠标到达菜单元素时显示不同的光标,我认为要做到这一点,您必须在创建菜单时在选项中添加 cursor='something' 选项

try:
  import tkinter as tk
except ImportError:
  import Tkinter as tk

class Settings:
    def __init__(self, master):
        # Elements of the menu
        self.master=master
        self.menu = tk.Menu(root, fg="red")
        self.subMenu = tk.Menu(self.menu, cursor="hand1")

    def openMenu(self):
        # Configuration of the menu
        self.menu.add_cascade(label="Options", menu=self.subMenu)
        self.addOptionsSubMenu()
        self.master.config(menu=self.menu)

    def addOptionsSubMenu(self):
        # Add elements at the sub menu
        self.subMenu.add_command(label="Quit", command=self.quit)
        self.subMenu.add_command(label="Do nothing", command=self.passa)

    # Quit the function
    def quit(self):
        exit()

    # Do nothing
    def passa(self):
        pass

root = tk.Tk()
app = Settings(root)
app.openMenu()
root.mainloop()

但是由于光标没有改变,我该怎么做呢?

【问题讨论】:

    标签: python tkinter tkinter-menu


    【解决方案1】:

    tkinter Menudocs 状态光标选项表示“The cursor that appears when the mouse is over the choices, but only when the menu has been torn off”。所以,我不认为你真的可以做你想做的事。只有当您的子菜单被分离(撕下)时,您才能看到光标的变化。这是一个演示。

    import tkinter as tk
    
    class Settings:
      def __init__(self, master):
        # Elements of the menu
        self.master=master
        self.menu = tk.Menu(root, fg="red")
        self.subMenu = tk.Menu(self.menu, cursor="plus")
    
      def openMenu(self):
        # Configuration of the menu
        self.menu.add_cascade(label="Options", menu=self.subMenu)
        self.addOptionsSubMenu()
        self.master.config(menu=self.menu)
    
      def addOptionsSubMenu(self):
        # Add elements at the sub menu
        self.subMenu.add_command(label="Quit", command=self.quit)
        self.subMenu.add_command(label="Do nothing", command=self.passa)
    
      # Quit the function
      def quit(self):
        exit()
    
      # Do nothing
      def passa(self):
        pass
    
    root = tk.Tk()
    app = Settings(root)
    app.openMenu()
    root.mainloop()

    【讨论】:

      猜你喜欢
      • 2019-03-14
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-02
      • 1970-01-01
      相关资源
      最近更新 更多