Python Tkinter:在状态栏中显示描述 [英] Python Tkinter: Display description in status bar

查看:1015
本文介绍了Python Tkinter:在状态栏中显示描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的Tkinter GUI,我已经创建了一个菜单和一个状态栏.但是,当鼠标悬停在菜单项上方时,如何显示说明?

For my Tkinter GUI, I have already created a Menu and a Status Bar. 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()  

推荐答案

因此,您要查找的是<<MenuSelect>>.

如果将self.helpMenu绑定到<<MenuSelect>>,例如在我的代码更新中:

If you bind the self.helpMenu to <<MenuSelect>>, like in my update of your code:

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

现在唯一需要跟踪的是什么索引对应于哪个菜单项.我建议每次添加菜单项时保持int递增,并通过dict使用每个索引来获取有关所需菜单项的任何相关信息. Tkinter不会通过statusBarUpdate回调中的条目告诉您很多信息(就像从菜单命令"About"中调用它一样),所以我认为您需要自己滚动它.

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. 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.

这篇关于Python Tkinter:在状态栏中显示描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆