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

2024/7/8 6:54:23

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

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

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

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 = 0self.application = 0self.width = widthself.height = heightself.colScale = self.width / self.colsself.rowScale = self.height / self.rowsself.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.

Answer

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

"""Do not use. Needed in Tk 3.6 and earlier."""

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

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.

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

import tkinter as tkroot = 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()
https://en.xdnf.cn/q/119526.html

Related Q&A

Standard Input having weird characters with them in different programming lanuage

I am getting confused with the standard input of these programming languages : [Note:] I added details about so many programming languages as the problem with all of them is same in this matter and my …

How to turn a numpy array to a numpy object?

I have a NumPy array as follows: [[[ 0 0]][[ 0 479]][[639 479]][[639 0]]]and I would like to convert it into something like so: [( 0 0)( 0 479)(639 479)(639 0), dtype=dtype([(x, <i2), (y…

recover all line from an attribute in a database in json

To simplify my problem, I have a base in json, and I recover all of my lines of json to put informations in a base. It seems easy for moments, but problem is that my json is not correctly writtenSo i…

Calculate eigen value in python as same way(order) in Matlab

This is the Matlab code which is returning eigenvector in V and eigenvalue in D. Consider C is 9*9 matrix then V is 9*9 matrix and D is 9*9 diagonal. matrix.[V,D] = eig(C);I want the same thing in Pyth…

Python: ctypes and Pointer to Structure

I am trying to make a pointer of a struct and then de-reference it. But its crashing. I have mimiced the behvior here with this simple code. from ctypes import * import ctypesclass File(Structure):_fie…

Python:Why readline() function doesnt work for file looping

I have the following code:#!/usr/bin/pythonf = open(file,r)for line in f:print line print Next line , f.readline() f.close()This gives the following output:This is the first lineNext line That was the …

How to replace cropped rectangle in opencv?

I have managed to cropped a bounding box with text, e.g. given this image:Im able to exact the following box:with this code: import re import shutilfrom IPython.display import Imageimport requests impo…

How can I read hexadecimal data with python?

I have this c# app that Im trying to cooperate with a app written in python. The c# app send simple commands to the python app, for instance my c# app is sending the following:[Flags]public enum GameRo…

Want to scrape all the specific href from the a tag

I have search the specific brand Samsung , for this number of products are search ,I just wanted to scrape all the href from the of the search products with the product name . enter code here import u…

Encryption code in def function to be written in python

need some help in the following code as it goes into infinite loop and does not validate user input: the get_offset is the function. Just edited need some help with the encryption part to be done in a …