Python Tkinter menubar类
我试图通过创建菜单栏及其内容的类来提高代码的效率。没有报告错误,但它不会成为标准。(我确实让它在没有上课的情况下工作,但似乎无法让它再次工作!) 这确实绘制了Tk窗口,但没有菜单栏选项,我哪里出错了?Python Tkinter menubar类,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,我试图通过创建菜单栏及其内容的类来提高代码的效率。没有报告错误,但它不会成为标准。(我确实让它在没有上课的情况下工作,但似乎无法让它再次工作!) 这确实绘制了Tk窗口,但没有菜单栏选项,我哪里出错了? 干杯。在\uuuu init\uuu函数中,只需创建一个菜单栏。 创建一个函数,该函数添加菜单并将命令作为元组列表接受: from Tkinter import * class MenuBar: #'player' is the name of the Tk window def __i
干杯。在
\uuuu init\uuu
函数中,只需创建一个菜单栏。创建一个函数,该函数添加菜单并将命令作为元组列表接受:
from Tkinter import *
class MenuBar: #'player' is the name of the Tk window
def __init__(self, parent):
self.menubar = Menu(parent)
self.Create()
def Create(self):
player.config(menu = self.menubar)
def add_menu(self, menuname, commands):
menu = Menu(self.menubar, tearoff = 0)
for command in commands:
menu.add_command(label = command[0], command = command[1])
if command[2]:
menu.add_separator()
self.menubar.add_cascade(label=menuname, menu=menu)
def onExit():
import sys
sys.exit()
def onOpen():
print 'Open'
player = Tk()
menubar = MenuBar(player)
fileMenu = menubar.add_menu("File", commands = [("Open", onOpen, True), ("Exit", onExit, False)])
player.mainloop()
它没有在Mac上使用2.7.11提供任何菜单项。我确实有onExit函数的代码,只是这里没有包含它。
from Tkinter import *
class MenuBar: #'player' is the name of the Tk window
def __init__(self, parent):
self.menubar = Menu(parent)
self.Create()
def Create(self):
player.config(menu = self.menubar)
def add_menu(self, menuname, commands):
menu = Menu(self.menubar, tearoff = 0)
for command in commands:
menu.add_command(label = command[0], command = command[1])
if command[2]:
menu.add_separator()
self.menubar.add_cascade(label=menuname, menu=menu)
def onExit():
import sys
sys.exit()
def onOpen():
print 'Open'
player = Tk()
menubar = MenuBar(player)
fileMenu = menubar.add_menu("File", commands = [("Open", onOpen, True), ("Exit", onExit, False)])
player.mainloop()