简体   繁体   English

Python Tkinter:在状态栏中显示描述

[英]Python Tkinter: Display description in status bar

For my Tkinter GUI, I have already created a Menu and a Status Bar. 对于我的Tkinter GUI,我已经创建了一个菜单和一个状态栏。 However, how to display description when mouse is over menu items? 但是,当鼠标悬停在菜单项上时如何显示说明?

Please run the following code (eg. when mouse is over menu item "About", status bar should write "Information": 请运行以下代码(例如,当鼠标悬停在菜单项“关于”上时,状态栏应显示“信息”:

from Tkinter import Tk, Frame, BOTH, Menu, Label, SUNKEN, X, BOTTOM

class Application(Frame):
   def __init__(self, parent):
      Frame.__init__(self, parent, background = "white")
      parent.configure(bg = "black")
      self.pack(fill = BOTH, expand = True, padx = 20, pady = 20)

      self.parent = parent

      # Maximize window
      self.screenWidth = self.parent.winfo_screenwidth() - 5
      self.screenHeight = self.parent.winfo_screenheight() - 110
      self.parent.geometry('%dx%d+%d+%d' % (self.screenWidth, self.screenHeight, 0, 0))
      self.parent.resizable(0, 0)

      # Status bar
      self.statusBar = StatusBar(self.parent)
      self.statusBar.pack(side = BOTTOM, fill = X)

      # Menu bar
      menubar = Menu(self.parent)
      self.parent.config(menu = menubar)

      self.commandMenu = Menu(menubar, tearoff = 0)
      self.commandMenu.add_command(label = "Rename", command = self.onRename)
      menubar.add_cascade(label = "Command", menu = self.commandMenu)

      self.helpMenu = Menu(menubar, tearoff = 0)
      self.helpMenu.add_command(label = "About", command = self.onAbout)
      menubar.add_cascade(label = "Help", menu = self.helpMenu)

   def onRename(self):
      pass
   def onAbout(self):
      pass

class StatusBar(Frame):
   def __init__(self, master):
      Frame.__init__(self, master)
      self.label = Label(self, bd = 1, relief = SUNKEN, anchor = "w")
      self.label.pack(fill=X)
   def set(self, format0, *args):
      self.label.config(text = format0 % args)
      self.label.update_idletasks()
   def clear(self):
      self.label.config(text="")
      self.label.update_idletasks()

def main():
   root = Tk()
   Application(root)
   root.mainloop()

if __name__ == '__main__':
   main()  

So, the thing you're looking for is the <<MenuSelect>> . 因此,您要寻找的是<<MenuSelect>>

If you bind the self.helpMenu to <<MenuSelect>> , like in my update of your code: 如果将self.helpMenu绑定到<<MenuSelect>> ,就像我在更新代码中那样:

__author__ = 'rcope'
from Tkinter import Tk, Frame, BOTH, Menu, Label, SUNKEN, X, BOTTOM

class Application(Frame):
   def __init__(self, parent):
      Frame.__init__(self, parent, background = "white")
      parent.configure(bg = "black")
      self.pack(fill = BOTH, expand = True, padx = 20, pady = 20)

      self.parent = parent

      # Maximize window
      self.screenWidth = self.parent.winfo_screenwidth() - 5
      self.screenHeight = self.parent.winfo_screenheight() - 110
      self.parent.geometry('%dx%d+%d+%d' % (self.screenWidth, self.screenHeight, 0, 0))
      self.parent.resizable(0, 0)

      # Status bar
      self.statusBar = StatusBar(self.parent)
      self.statusBar.pack(side = BOTTOM, fill = X)

      # Menu bar
      menubar = Menu(self.parent)
      self.parent.config(menu = menubar)

      self.commandMenu = Menu(menubar, tearoff = 0)
      self.commandMenu.add_command(label = "Rename", command = self.onRename)
      menubar.add_cascade(label = "Command", menu = self.commandMenu)

      self.helpMenu = Menu(menubar, tearoff = 0)
      self.helpMenu.add_command(label = "About", command = self.onAbout)
      menubar.add_cascade(label = "Help", menu = self.helpMenu)
      self.helpMenu.bind("<<MenuSelect>>", self.statusBarUpdate)

   def onRename(self):
      pass
   def onAbout(self):
      pass

   def statusBarUpdate(self, event=None):
       print "Status Bar Update Called"
       if self.parent.call(event.widget, "index", "active") == 0:
           self.statusBar.set("About This Application")
       else:
           self.statusBar.set("")

class StatusBar(Frame):
   def __init__(self, master):
      Frame.__init__(self, master)
      self.label = Label(self, bd = 1, relief = SUNKEN, anchor = "w")
      self.label.pack(fill=X)
   def set(self, format0, *args):
      self.label.config(text = format0 % args)
      self.label.update_idletasks()
   def clear(self):
      self.label.config(text="")
      self.label.update_idletasks()

def main():
   root = Tk()
   Application(root)
   root.mainloop()

if __name__ == '__main__':
   main()

The only thing you need to keep track of now, is what index corresponds to what menu item. 现在唯一需要跟踪的是什么索引对应什么菜单项。 I would recommend keeping an int you increment each time you add a menu entry, and using each index via a dict to get whatever relevant information about the menu item you need. 我建议每次添加菜单项时保持int递增,并通过dict使用每个索引来获取有关所需菜单项的任何相关信息。 Tkinter doesn't tell you a lot through the entry in the statusBarUpdate callback (like that this was called from the menu command labeled "About"), so you need to roll this yourself, I think. Tkinter不会通过statusBarUpdate回调中的条目告诉您很多信息(例如,这是通过菜单命令“ About”调用的),因此,我认为您需要自己滚动一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM