Python 3 Programming Tutorial - Tkinter menu bar

preview_player
Показать описание
Now that we understand buttons and events, it's time to learn about menu bars in Python's tkinter module. Adding menus and items to those menus can be confusing at times, but hopefully I can help keep it simple.

Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Рекомендации по теме
Комментарии
Автор

Hi Sentdex,

I'm sure you have heard this before, but a lot of tutorials differentiate by saying myMenu instad of menu. This makes it easier to see which type of menu you are referencing.

Thanks a lot for your tutorials! They're really helpful.

Thonss
Автор

If you want to avoid using "menu = Menu(self.master)" you can always use "menu1 = Menu(self.master)" that way its clearer when you reference menu1 that its user defined. I'm not sure if that violates any best practices in python though.

Also, thanks for making all the tutorials.They are good.

milliamp
Автор

in the function client_exit(self), to avoid Python alerting that your program is still running and whether to kill it, try:

root.destroy()
sys.exit()

instead of:

exit()

oldaccount
Автор

Hey Harrison, after typing the below code 

from tkinter import *
class Window (Frame):
def _init_(self, master = None):
Frame.__init__(self, master)

self.master = master
self.init_window()

def init_window(self):
self.master.title("GUI")
self.pack(fill = BOTH, expand  = 1)
#quitButton = Button(self, text="Quit", command = self.client_exit)
#quitButton.place(x = 0, y = 0)

menu = Menu(self.master)

self.master.config(menu = menu)

file = Menu(menu)
file.add_command(label = 'Exit', command = self.client_exit)
menu.add_cascade(label = 'File', menu = file)
def client_exit():
exit()


root = Tk ()
root.geometry("400x300")
app = Window (root)
root.mainloop()

I cannot see the file button appear nor did i see the quit button appear in the window,  
I do not get any type of error but just a blank window.
Please help me out, appreciate your help.

Thanks

varunv
Автор

This was a quite while since you made the video, but a tip not make it to confusing maybe name your variables something like myMenu or mainMenu instead of menu so it wont get mixed up with Menu

capitangreen
Автор

Thank you for sharing this!  This explains this concept quite well.  What about sub-menus?  For example, how could one do a "Share" menu below with sub-menus?

"File" menu
> Open
> Save
> Share...
by E-mail
by FaceBook
by DropBox

LearnWithByron
Автор

menu = file = Menu(menu) menu menu --- menuception

Veria
Автор

Hi Harrison,
I've been learning Python 3 for nearly 4 months now, but I still encounter more things to learn. A good example would be modules; every time I see a function, I don't know what it should do and that just leaves me to learning the entire script. Now if I did that with every single module, that would be very time consuming...

My question is: Should I study each one and get a better understanding, or should I just ignore them and let them do their thing?
I'm stuck between learning everything, or being completely oblivious.

eltriplej
Автор

Great videos. However, I have an issue with this example. I have created the example by cutting and pasting the code from your tutorial site. When run the Window is displayed but the menu is not. I am running Python 3.8 on a macOS 10.13.

paulwest
Автор

Thanks bro, but in my case, i use ubuntu and when I trying run, it not works like that, i can't see File menu or Undo, ..

nickfk_flutter
Автор

Me still finding this useful 7years after

btl
Автор

on the net when someone want to learn python they all say sentdex thank you very much

bilelmalek
Автор

Hi.. I'm kind of new to both python 3 and tkinter, and, I realize that you use classes for making the GUI while most of all the other tutorials just put them out in the open. Does this provide any great advantage in like big programs? If so, then how?

shrinivasiyengar
Автор

hi sentdex, i watched your video and i saw when menu create i has a horizontal line, i don't want show it, can u tell me how to fix this??

hieudao
Автор

my code is not running. No errors either. python v 3.7(anaconda) windows 10. Same exact code as you. what might be the problem?

DreamerSurjit
Автор

from tkinter import *

myGui = Tk()

myGui.title("My First Window")


mymenu = Menu(myGui)




myGui.config(menu=mymenu)

myGui.mainloop()

This is working on windows but not working on mac, what should I do on mac to work this

TheKailash
Автор

I run the code and it works fine, but then the file drop down appears in my main window's tab instead of the new window's tab. How to fix this?

AlphaNiner-ykkh
Автор

What do I do if I want a Help menu which directs me to some webpage- say google for now. However, I do not want this to happen with a drop down. I just want the one button on the main-Menu which directs me.. Please help!

spdrajiv
Автор

Hi thanks for this wonderful video. i have question. how can i build a menu inside a menu. for example in file menu >> Recent file menu >> some icons

abdoslab
Автор

I want to add another option of "New window" which can call the window class and new object window pops up. So how can I do that .. I added a line but I am not about the command in that:
"
file.add_command(label='New Window', command = Tk)
"
Tk above in command gives me the new window but it doesn't show the menu in it, so what can I do for that ?

vineetsansi