python-2.7 - 如何为不同的 Tkinter [子] 菜单执行不同的回调?

标签 python-2.7 tkinter callback menu submenu

我有一个带主菜单栏的 Tkinter GUI,使用 Tkinter 的 Menu 小部件。我想在发布子菜单(通过 .add_cascade() 从它级联的另一个菜单项)之前执行代码,以便我可以在显示之前动态更改其内容。我使用 Menu 的 postcommand 参数进行了这项工作,但我注意到使用它的效率非常低;单击任何 子菜单时调用所有 子菜单的后命令回调,而不仅仅是为回调而创建的特定子菜单。即使点击没有菜单项的菜单栏也会执行所有回调,即使没有创建子菜单。

这是 Menu 模块及其后命令参数的预期行为吗?我不明白为什么在为下拉菜单创建单独的菜单实例后仍然会发生这种情况。

我已经尝试连接到 Tk.Menu 的 native 方法,但是当简单地单击菜单栏项目以调出级联菜单时,它们都没有被调用。即使 .add_cascade() 接受一个“command”参数,如果 .add_cascade() 的“menu”参数不包含或者它是一个 lambda 表达式(两者都不会产生子菜单),它只会调用由它提供的可调用对象单击该项目时显示)。 (您可以使用下面的 test() 函数看到这一点。)

这是一个显示此行为的简单应用:

import Tkinter as Tk
import time


def test(): print 'test'

class firstMenu( Tk.Menu ):

    def __init__( self, parent, tearoff=False ):
        Tk.Menu.__init__( self, parent, tearoff=tearoff, postcommand=self.repopulate )

    def repopulate( self ):
        print 'repopulating firstMenu'
        time.sleep( 2 ) # Represents some thinking/processing

        # Clear all current population
        self.delete( 0, 'last' )

        # Add the new menu items
        self.add_command( label='Option 1.1' )
        self.add_command( label='Option 1.2' )


class secondMenu( Tk.Menu ):

    def __init__( self, parent, tearoff=False ):
        Tk.Menu.__init__( self, parent, tearoff=tearoff, postcommand=self.repopulate )

    def repopulate( self ):
        print 'repopulating secondMenu'
        time.sleep( 2 ) # Represents some thinking/processing

        # Clear all current population
        self.delete( 0, 'last' )

        # Add the new menu items
        self.add_command( label='Option 2.1' )
        self.add_command( label='Option 2.2' )


class Gui( object ):

    def __init__( self ): # Create the TopLevel window
        root = Tk.Tk()
        root.withdraw() # Keep the GUI minimized until it is fully generated
        root.title( 'Menu Test' )

        # Create the GUI's main program menus
        menubar = Tk.Menu( root )
        menubar.add_cascade( label='File', menu=firstMenu( menubar ), command=test )
        menubar.add_cascade( label='Settings', menu=secondMenu( menubar ) )
        root.config( menu=menubar )

        root.deiconify() # Brings the GUI to the foreground now that rendering is complete

        # Start the GUI's mainloop
        root.mainloop()
        root.quit()


if __name__ == '__main__': Gui()

如果您单击菜单栏上的任意位置,则会调用两个命令后回调。当您单击相应的菜单项时,我只需要(并且期望)调用其中一个。

我不确定它是否相关,但我还在另一个小部件上使用与上下文菜单相同的菜单项。因此他们的 .post() 方法也需要能够在显示菜单之前触发相同的回调。

如果您有任何见解,请提前致谢。

最佳答案

这是一个非常棘手的问题,但我终于找到了解决方案。经过大量搜索、无数次失败的实验以及更多搜索之后,我遇到了虚拟事件 <<MenuSelect>>这个关键的代码行:print tk.call(event.widget, "index", "active") ,Michael O' Donnell 指出,here .

尝试使用它的第一个奇怪部分是 event.widget在这种情况下不是小部件对象的实例,它是一个 tcl/tk 路径名字符串,例如'.#37759048L'。 (这似乎是 Tkinter 中的一个错误,因为我测试过的其他虚拟事件 -TreeviewSelect 和 NotebookTabChanged- 如预期的那样包括实际的小部件实例。)无论如何,print tk.call(event.widget, "index", "active") 都可以使用 tcl/tk 字符串。命令;返回当前事件菜单项的索引,它很大。

使用 MenuSelect 事件带来的第二个问题是在正常遍历菜单时它会被调用多次。单击一个菜单项会调用它两次,将鼠标移动到相邻的菜单项,或将鼠标移动到子菜单然后返回主菜单项,也会调用它两次。离开菜单也可以。但这可以通过向 Menu 类添加一个标志并向事件处理程序添加一些逻辑来很好地清理。这是完整的解决方案:

import Tkinter as Tk
import time


class firstMenu( Tk.Menu ):

    def __init__( self, parent, tearoff=False ):
        Tk.Menu.__init__( self, parent, tearoff=tearoff )
        self.optionNum = 0 # Increments each time the menu is show, so we can see it update
        self.open = False

    def repopulate( self ):
        print 'repopulating firstMenu'

        # Clear all current population
        self.delete( 0, 'last' )

        # Add the new menu items
        self.add_command( label='Option 1.' + str(self.optionNum+1) )
        self.add_command( label='Option 1.' + str(self.optionNum+2) )
        self.optionNum += 2


class secondMenu( Tk.Menu ):

    def __init__( self, parent, tearoff=False ):
        Tk.Menu.__init__( self, parent, tearoff=tearoff )
        self.optionNum = 0 # Increments each time the menu is show, so we can see it update
        self.open = False

    def repopulate( self ):
        print 'repopulating secondMenu'

        # Clear all current population
        self.delete( 0, 'last' )

        # Add the new menu items
        self.add_command( label='Option 2.' + str(self.optionNum+1) )
        self.add_command( label='Option 2.' + str(self.optionNum+2) )
        self.optionNum += 2


class Gui( object ):

    def __init__( self ): # Create the TopLevel window
        self.root = Tk.Tk()
        self.root.withdraw() # Keep the GUI minimized until it is fully generated
        self.root.title( 'Menu Tests' )

        # Create the GUI's main program menus
        self.menubar = Tk.Menu( self.root )
        self.menubar.add_cascade( label='File', menu=firstMenu( self.menubar ) )
        self.menubar.add_cascade( label='Settings', menu=secondMenu( self.menubar ) )

        self.root.config( menu=self.menubar )
        self.root.deiconify() # Brings the GUI to the foreground now that rendering is complete

        # Add an event handler for activation of the main menus
        self.menubar.bind( "<<MenuSelect>>", self.updateMainMenuOptions )

        # Start the GUI's mainloop
        self.root.mainloop()
        self.root.quit()

    def updateMainMenuOptions( self, event ):
        activeMenuIndex = self.root.call( event.widget, "index", "active" ) # event.widget is a path string, not a widget instance

        if isinstance( activeMenuIndex, int ):
            activeMenu = self.menubar.winfo_children()[activeMenuIndex]

            if not activeMenu.open:
                # Repopulate the menu's contents
                activeMenu.repopulate()
                activeMenu.open = True

        else: # The active menu index is 'none'; all menus are closed
            for menuWidget in self.menubar.winfo_children():
                menuWidget.open = False


if __name__ == '__main__': Gui()

最终结果是每个菜单的代码生成其内容,通过.repopulate() , 只被调用一次,并且只有在实际要显示该特定菜单时才调用。在离开并重新打开整个主菜单之前,不会再次调用该方法。通过键盘导航时也适用。

关于python-2.7 - 如何为不同的 Tkinter [子] 菜单执行不同的回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55753828/

相关文章:

python - 我们可以在 web2py 应用程序代码中使用 java 代码吗?

python - 使用pyinstaller打包后,python中的程序无法运行

java - 在 asynctask 线程 Android 中运行 View 监听器回调代码

c# - 从 C++ 到 C# 的反向调用

字典列表上的 Python 多处理

python - 如何将日期转换为 0001-01-01 中的天数?

python - 模拟不适用于模块功能

python - TypeError: 'builtin_function_or_method' 对象不可下标

Python 多处理将子进程的 stdout 重定向到 Tkinter Text

javascript - 使用变量传递回调