0

im working on a project but its not going so well.
this code:

from tkinter import *

def NewFile():
    new = Label(root, text="about \n")
def OpenFile():
    openf = Label(root, text="about \n")
def About():
    about = Label(root, text="about \n")

root = Tk()
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=NewFile)
filemenu.add_command(label="Open...", command=OpenFile)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)

helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=About)
body = Label(root, text="")
mainloop()

Doesnt work how I need it.
its suposed to write certant messages when you click file > new, file > open and help > about.

IT DOES NOTHING

how can I make it do what I want?

Bloxy Craft
  • 131
  • 1
  • 3
  • 12

2 Answers2

1

You are not using geometry managers(pack/grid/place) on your widgets so tkinter doesn't show those to you.

Also, instead of creating new labels on every click, you can either create all three in global scope and pack&forget on clicks or just create one Label and change its value depending on your needs.

Lafexlos
  • 7,618
  • 5
  • 38
  • 53
1
from Tkinter import *



root = Tk()
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
my_label = Label(root, text="Select Menu")
my_label.place(x=10,y=10)

def my_command(query):
    my_label.config(text=query)

menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=lambda x = "New":my_command(x))
filemenu.add_command(label="Open...", command=lambda x = "Open...":my_command(x))
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.destroy)

helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=lambda x = "About...":my_command(x))
body = Label(root, text="")
mainloop()

@Lafexlos is correct. You can use all python types(list,dict,var and more...) on GUI design, most important point how to manage all GUI elements, so all need an accessible status.

Don't destroy any GUI element, change and reuse it.

Don't include any external command to mainloop(like:file,network, etc.)

All GUI application required 3 critical section : INIT >> BUILD >> RUN Otherwise you got a lot painful.

Use GUI elements text as variable, of-course if all element is accesible !

I hope helpful and accept @Lafexlos answer not my ! This code work on Python2.7

dsgdfg
  • 1,492
  • 11
  • 18