python - Menu 类中的 Tkinter 选项光标

标签 python tkinter tkinter-menu

我试图在鼠标到达菜单元素时显示不同的光标,我认为要做到这一点,您必须在选项中添加 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()

但是光标没有改变,我该怎么办?

最佳答案

docs对于 tkinter Menu声明光标选项表示“ 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, <b>cursor="plus"</b>)

  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()

Demo

关于python - Menu 类中的 Tkinter 选项光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54124903/

相关文章:

python - 通过 event_generate 传递附加参数?

python - 如何将 Action 绑定(bind)到 python 中的 tkinter TreeView 的标题?

Python TKInter : AttributeError: 'NoneType' object has no attribute 'create_image'

python - Tkinter:将图标添加到菜单项中

python - 使用覆盖率运行 Django 测试 PyCharm

c++ - Cython 扩展类 : "incorrect checksum for freed object" after __dealloc__ method

python - 如何获取视频flash文件的持续时间?

python - 获取字典/json中键的类型