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

Posted

技术标签:

【中文标题】Python Tkinter:在状态栏中显示描述【英文标题】:Python Tkinter: Display description in status bar 【发布时间】:2014-05-24 15:01:01 【问题描述】:

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

请运行以下代码(例如,当鼠标在菜单项“关于”上时,状态栏应该写“信息”:

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

【问题讨论】:

【参考方案1】:

所以,您要查找的是<<MenuSelect>>

如果你将 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()

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

【讨论】:

以上是关于Python Tkinter:在状态栏中显示描述的主要内容,如果未能解决你的问题,请参考以下文章

在 OS X 菜单栏中更改 Tkinter 应用程序的标题

如何在 guizero/tkinter python 3 中将图标添加回任务栏?

使 Tkinter 窗口显示在任务栏中

帮助 Tkinter Alpha

如何使用 python 3 和 tkinter 模块从右到左对齐菜单栏中的项目?

python tkinter 如何将tk界面中标题栏中的tk红色字样去掉,或者换成其他的字