Why isn't the menu showing up? - Tkinter - python

I'm trying to do a menu using tkinter. But when I run the code, the window appears, but the menu doesn't. I'm getting no errors.
Here is my code:
from tkinter import *
root = Tk()
root.geometry("500x300")
menu = Menu(root)
file_menu = Menu(menu, tearoff=0)
file_menu.add_command(label="New")
file_menu.add_command(label="Open")
file_menu.add_command(label="Save")
menu.add_cascade(label="File", menu=file_menu)
menu
root.mainloop()

You haven't added it to the root window. You need to do the following after creating menu:
root.configure(menu=menu)

Related

Trying to make a tkinter menu button that do multiple tasks

I've been trying to make a button in menu bar in the tkinter app and can't seem to figure out how to make this button make multiple tasks. I've tried the following, it forgets the frame correctly but don't execute the carr function.
from tkinter import *
root = Tk()
menu_bar = Menu(root)
file_menu = Menu(menu_bar, tearoff = 0)
file_menu.add_command(label="Carré", command=lambda:[frame.pack_forget(),carr])
menu_bar.add_cascade(label="Séléction", menu=file_menu)
root.config(menu=menu_bar)
root.mainloop()
The proper solution is to create a function for the button.
def do_carr():
frame.pack_forget()
carr()
...
file_menu.add_command(label="Carré", command=do_carr)

My browse window in tkinter goes under the toplevel tkinter window where topmost enabled

I will try to simplify my question here. My problem is like, on my first window of tkinter I have a button that opens another tkinter window. You can say it a second window to keep it on top I use win2.attributes('-topmost', True). Then I have a browse button to import file from the computer but when I click it goes under the window 2.
Following is my code.
from tkinter import *
from tkinter.filedialog import askopenfilename
root = Tk()
root.title("Base Window")
root.geometry("300x300")
def browse():
file_to_open = askopenfilename()
def new_window():
win2 = Toplevel(root)
win2.title("Window 2")
win2.geometry("300x300")
win2.attributes('-topmost', True)
Button2 = Button(win2, text="Browse", command=browse).pack()
button1 = Button(root, text="New window", command=new_window).pack()
root.mainloop()
My browse window is going under the window 2. Can you please suggest me the best way to keep it on top. I am sharing the screenshot of the problem as well
Starting code
Click on new window
Click browse button and the browse window is hidden
Cheers
You need to set the parent option of askopenfilename() to the toplevel window win2:
from tkinter import *
from tkinter.filedialog import askopenfilename
root = Tk()
root.title("Base Window")
root.geometry("300x300")
def browse(parent):
file_to_open = askopenfilename(parent=parent)
def new_window():
win2 = Toplevel(root)
win2.title("Window 2")
win2.geometry("300x300")
win2.attributes('-topmost', True)
# pass 'win2' to browse()
Button2 = Button(win2, text="Browse", command=lambda:browse(win2)).pack()
button1 = Button(root, text="New window", command=new_window).pack()
root.mainloop()

Add commands to the "Python" menu with Tkinter on macOS - Python 3

I have a Tkinter app that contains a menu. I can add cascades and commands in these cascades but can I add a command to the original app menu? The one that is created automatically on macOS and gets the name of the app:
Here is my code:
from tkinter import *
root = Tk()
menubar = Menu(root)
# I can create a cascade
cascade1 = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Cascade 1", menu=cascade1)
# And add a command to this cascade
cascade1.add_command(label="Command 1")
# How to add a command to the "Python" menu
# ???
root.config(menu=menubar)
root.mainloop()
I don't think it's possible to add a command anywhere on that menu except at the very top. However, you can add items at the top by creating a menu with the name "apple". Items you add to that menu will appear at the top of the application menu.
Note: you must configure this menu before assigning the menubar to the root window.
import tkinter as tk
root = tk.Tk()
menubar = tk.Menu(root)
appmenu = tk.Menu(menubar, name='apple')
menubar.add_cascade(menu=appmenu)
appmenu.add_command(label='My Custom Command')
appmenu.add_separator()
root.configure(menu=menubar)
root.mainloop()
For reference, see Special menus in menubars in the the tcl/tk documentation.

Tkinter menu is not appearing on MacOS 11.2.2

I am trying to create an application to show real-time data in a study on electrical systems. The menu will be used to open information on history of each of the graphs shown on the "front page" and will also include a quit option. When I create the menu and add a couple items to it, it does not show up when I run the application. Unfortunately in my searches, all the alternatives I have tried do not show up either.
from tkinter import *
from tkinter import ttk
class PicoGridInterfacing(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.grid()
menubar = Menu(root)
menu = Menu(menubar)
menubar.add_cascade(label="Menu", menu=menu)
menu.add_command(label="History", command=None)
menu.add_command(label="Quit", command=root.quit)
batteryData = Frame(root)
batteryData.grid(row=1, column=0)
Label(batteryData, text="Hello!").grid()
root = Tk()
root.title("Electric Power Systems Lab Pico Grid Interfacing")
app = PicoGridInterfacing(master=root)
root.mainloop()
Here's a screenshot of the output I see:
Screenshot of output
Any help would be greatly appreciated, thank you!
Try this:
from tkinter import *
from tkinter import ttk
class PicoGridInterfacing(Frame):
def __init__(self, master=None):
super().__init__(master)
# master = master # `super().__init__(master)` already does that so its useless
# Creating the menu
menubar = Menu(root)
menu = Menu(menubar, tearoff=False)
menubar.add_cascade(label="Menu", menu=menu)
menu.add_command(label="History", command=None)
menu.add_command(label="Quit", command=root.destroy)
# Tell the `Tk()` that there is a menu that it need to display:
master.config(menu=menubar)
batteryData = Frame(self)
batteryData.grid(row=1, column=0)
Label(batteryData, text="Hello!").grid()
root = Tk()
root.title("Electric Power Systems Lab Pico Grid Interfacing")
app = PicoGridInterfacing(master=root)
# When you inherit from `Frame` you always what the user to call `.grid`
app.grid()
root.mainloop()
You need to tell the tkinter.Tk() that there is a menu otherwise it will ignore it. Also when inheriting from tkinter.Frame it's the caller's job to call .grid (it's convention).
You need to attach the menu bar to the root window.
So do this: root.config(menu=menubar)

How to colour or remove the border of a tkinter Menu

In Tkinter, I have been fiddling with the Menu widget, but I haven't been successful in colouring my widgets in. The border of the Tkinter Menu widget stays grey no matter what I do:
Is there no other option but to leave it grey?
Code Snippet:
from tkinter import *
root = Tk()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New")
filemenu["bg"]="yellow"
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
root.mainloop()

Categories

Resources