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.
Related
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)
How do I open a new window using a link in tkinter .
(For eg : in a login window i want to add a link that says "New user ? click here" and when I click on "click here" it takes me to the register window .
Please help me
enter image description here
[1]: https://i.stack.imgur.com/K5GV0.png
Please click the above link to see the image
Creating new toplevel windows works almost exactly the same as creating new widgets.
Toplevel windows are created using the Toplevel function:
t = Toplevel(parent)
Unlike regular widgets, you don't have to "Grid" a toplevel fo it to appear on screen. Once you've created a toplevel you can add children widgets within and grid them like in the main window. In other words toplevel behaves exactly like the automatic created root window.
To destroy a window use the method:
window.destroy()
You can open new windows in tkinter with the tkinter.Toplevel() command.
import tkinter as tk
class Gui:
"""Gui class"""
def __init__(self):
self.root = tk.Tk()
self.new_window = tk.Button(master=self.root, text="Open new window", width=20, pady=4, command=self.new_window)
self.new_window.pack()
self.root.mainloop()
def new_window(self):
"""Create a new top level window"""
new_window = tk.Toplevel()
tk.Label(master=new_window, text="This is a new window").pack()
if __name__ == '__main__':
Gui()
You can create a function to open a new window and then bind it to that Label, for example:
import tkinter as tk
def newWindow():
# Window object (top level)
newWindow = Toplevel(master)
# Title
newWindow.title("New Window 1")
# Geometry
newWindow.geometry("300x300")
root = tk.Tk()
label = tk.Label(text="Hello!", width=50, height=10, master=root)
label.pack()
label.bind("<Button-1>", newWindow)
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)
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)
I'm trying to create a number of frames to be accessed through the use of the menu widget. When using the menu, you can click on one of the commands - it would bring up a frame and the menu widget should still be at the top, so you can easily decide where to go.
I'm trying to make use of the option menu widget within a function which is called after a login page, therefore I'm using the top level method within it. When attempting to do this option menu I encountered a few problems and I'm currently stuck as well as not understanding what's wrong with the code, so I was hoping someone would tell me what's wrong with it.
CoreContent = function named
myGUI = main root
def CoreContent():
#Building core content/structure
myGUI.withdraw() # This is the main root that I remove after user logs in
CoreRoot = Toplevel(myGUI, bg="powderblue") # Toplevel
CoreRoot.title("titletest")
CoreRoot.geometry('300x500')
CoreRoot.resizable(width=False, height=False)
#Creating drop-down menu
menu = Menu(CoreRoot)
CoreRoot.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="test one", command=lambda: doNothing()) # Problem
filemenu.add_command(label="soon")
filemenu.add_separator()
filemenu.add_command(label="Exit")
I'm confused how and where I should create the frames to add as a command to make use of within the option menu widget.
For a clear description of how to switch between frames in Tkinter, check this link: Switch between two frames in tkinter
To do it from a menu, you could write something like this:
import tkinter as tk
# method to raise a frame to the top
def raise_frame(frame):
frame.tkraise()
# Create a root, and add a menu
root = tk.Tk()
menu = tk.Menu(root)
root.config(menu=menu)
filemenu = tk.Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="test one", command=lambda: raise_frame(f1))
filemenu.add_command(label="test two", command=lambda: raise_frame(f2))
# Create two frames on top of each other
f1 = tk.Frame(root)
f2 = tk.Frame(root)
for frame in (f1, f2):
frame.grid(row=0, column=0, sticky='news')
# Add widgets to the frames
tk.Label(f1, text='FRAME 1').pack()
tk.Label(f2, text='FRAME 2').pack()
# Launch the app
root.mainloop()