python3在tkinter中向Frame添加菜单栏有什么区别

python3在tkinter中向Frame添加菜单栏有什么区别

本文介绍了python2和python3在tkinter中向Frame添加菜单栏有什么区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 python 上移植一个项目 2to3,但卡在 tkinter 中.

I'm trying to porting a project 2to3 on python, and stuck in tkinter.

在python2中,在tkinter中给Frame添加菜单栏是没有问题的,

In python2, there is no problem with adding menu bar to Frame in tkinter,

但是python3发生了属性错误.(Frame 对象没有属性 'tk_menuBar')

but python3 occured attribute error. (Frame object has no attribute 'tk_menuBar')

python2 和 python3 在 tkinter 中向 Frame 添加菜单栏有什么区别吗?

Is there any differences between python2 and python3 about adding menu bar to Frame in tkinter?

class TkMap(Map, tkinter.Tk):
""" Map with Tkinter GUI functions """
def __init__(self, cols, rows, value,
             width, height, widthMM, heightMM,
             title, menu = None, keybindings = []):
    """ TkMap extends Map and Tkinter """
    Map.__init__(self, cols, rows, widthMM, heightMM)
    tkinter.Tk.__init__(self)
    self.title(title)
    if menu == None:
        menu = [('File',[['Exit',self.destroy]])]
    keybindings.append( ("<Configure>", self.changeSize))
    self.menuButtons = {}
    self.debug = 0
    self.application = 0
    self.width = width
    self.height = height
    self.colScale = self.width / self.cols
    self.rowScale = self.height / self.rows
    self.addMenu(menu)

def addMenu(self, menu):
    """ Create a menu """
    self.mBar = tkinter.Frame(self,relief=tkinter.RAISED,borderwidth=2)
    self.mBar.pack(fill=tkinter.X)

    *for entry in menu:
        self.mBar.tk_menuBar(self.makeMenu(self.mBar, entry[0],entry[1]))*

    self.mBar.pack(side = "top")

附注.这是我的第一个问题,所以如果你指出我关于不礼貌的错误,我将不胜感激.

PS. It's my first question, so i will be appreciated that if you point out my mistake about bad manners.

推荐答案

你不应该在 python 2 或 python 3 中使用 tk_menuBar.该函数的文档字符串是这样说的:

You should not be using tk_menuBar in either python 2 or 3. The docstring for that function says this:

"""请勿使用.在 Tk 3.6 及更早版本中需要."""

注意:tk 3.6 在 90 年代初就过时了.

Note: tk 3.6 went obsolete back in the early 90's.

无法将菜单附加到 Frame 小部件.您可以添加 Menubutton 的实例来模拟菜单栏,但不会得到真正的菜单栏.

There is no way to attach a menu to a Frame widget. You can add instances of Menubutton to simulate a menubar, but you won't get a real menubar.

您可以通过配置menu 属性将Menu 附加到根窗口或Toplevel 的实例.

You can attach a Menu to the root window or to instances of Toplevel by configuring the menu attribute.

import tkinter as tk

root = tk.Tk()

menubar = tk.Menu()
fileMenu = tk.Menu()
editMenu = tk.Menu()
viewMenu = tk.Menu()

menubar.add_cascade(label="File", menu=fileMenu)
menubar.add_cascade(label="Edit", menu=editMenu)
menubar.add_cascade(label="View", menu=viewMenu)

root.configure(menu=menubar)

root.mainloop()

这篇关于python2和python3在tkinter中向Frame添加菜单栏有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:13