Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75729 > unrolled thread

Tkinter menu crash

Started byNicholas Cannon <nicholascannon1@gmail.com>
First post2014-08-05 05:15 -0700
Last post2014-08-06 00:13 -0400
Articles 6 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Tkinter menu crash Nicholas Cannon <nicholascannon1@gmail.com> - 2014-08-05 05:15 -0700
    Re: Tkinter menu crash Terry Reedy <tjreedy@udel.edu> - 2014-08-05 17:43 -0400
      Re: Tkinter menu crash Nicholas Cannon <nicholascannon1@gmail.com> - 2014-08-05 15:28 -0700
        Re: Tkinter menu crash Terry Reedy <tjreedy@udel.edu> - 2014-08-05 19:27 -0400
          Re: Tkinter menu crash Nicholas Cannon <nicholascannon1@gmail.com> - 2014-08-05 16:33 -0700
            Re: Tkinter menu crash Terry Reedy <tjreedy@udel.edu> - 2014-08-06 00:13 -0400

#75729 — Tkinter menu crash

FromNicholas Cannon <nicholascannon1@gmail.com>
Date2014-08-05 05:15 -0700
SubjectTkinter menu crash
Message-ID<1a31faea-eea6-4b1d-8dc1-185f13348621@googlegroups.com>
Ok so the first part of the program(until the start of the menu) worked fine. It ran and did what I wanted it to do. I wanted to then implement a new menu(for practise) and then it crashes. Don't know why but it just crashes. (also tips on the code will be appreciated and I gave just started Tkinter programming)

Here is the code:


from Tkinter import *
import tkMessageBox as tm

def submit():
    #message box with yes no 
    tm.askyesno(title='Submit Text', message='Are you sure....')
    
def info():
    tm.showinfo(title='About', message='Just a test Tkinter UI sample')
    

    #getting the text from the entrybox and
    #packs it into a label
    mtext = text.get()
    label1 = Label(app, text=mtext)
    label1.pack()
    
#root window setup
root = Tk()
root.geometry('480x480+200+200')
root.title('Basic Tk UI')

#frame set up
app = Frame(root)
app.pack()

#variable and entry box set up
text = StringVar()
entry = Entry(app, textvariable=text)
entry.pack()

#button set up
button1 = Button(app, text='Submit text', command= submit)
button1.pack()

#menu construction
menubar = Menu(root)

filemenu = Menu(menubar)
filemenu.add_command(label='About', command= info)
filemenu.add_command(label='Quit', command= root.destroy)
filemenu.add_cascade(label='TK UI Sample', menu=filemenu)

root.config(menu=menubar)



#loop to listen for events
root.mainloop()

[toc] | [next] | [standalone]


#75760

FromTerry Reedy <tjreedy@udel.edu>
Date2014-08-05 17:43 -0400
Message-ID<mailman.12677.1407275016.18130.python-list@python.org>
In reply to#75729
On 8/5/2014 8:15 AM, Nicholas Cannon wrote:
> Ok so the first part of the program(until the start of the menu) worked fine. It ran  and did what I wanted it to do.

What x.y.z version of Python. How did you run it, exactly?

> I wanted to then implement a new menu(for practise) and then it crashes. Don't know why but it just crashes.

If you ran from Idle editor on Windows, start Idle with 'python -m 
idlelib' in Command Prompt to see tk error messages.

However, when I pasted code into 3.4.1 Idle Editor, changed first two 
lines to

from tkinter import *
from tkinter import messagebox as tm

and ran -- no crash, no error message, no menu. I entered text into box, 
clicked Submit text, and OK on popup, and nothing happens.
See below for why no menu.

When you ask about a problem, please reduce code to the minimum that 
exhibits the problem for you.

> (also tips on the code will be appreciated and I gave just started Tkinter programming)

A different issue.

> Here is the code:
>
>
> from Tkinter import *
> import tkMessageBox as tm
>
> def submit():
>      #message box with yes no
>      tm.askyesno(title='Submit Text', message='Are you sure....')
>
> def info():
>      tm.showinfo(title='About', message='Just a test Tkinter UI sample')
>
>      #getting the text from the entrybox and
>      #packs it into a label
>      mtext = text.get()
>      label1 = Label(app, text=mtext)
>      label1.pack()
>
> #root window setup
> root = Tk()
> root.geometry('480x480+200+200')
> root.title('Basic Tk UI')
>
> #frame set up
> app = Frame(root)
> app.pack()
>
> #variable and entry box set up
> text = StringVar()
> entry = Entry(app, textvariable=text)
> entry.pack()
>
> #button set up
> button1 = Button(app, text='Submit text', command= submit)
> button1.pack()
>
> #menu construction
> menubar = Menu(root)
>
> filemenu = Menu(menubar)
> filemenu.add_command(label='About', command= info)
> filemenu.add_command(label='Quit', command= root.destroy)
> filemenu.add_cascade(label='TK UI Sample', menu=filemenu)

Adding filemenu as a submenu of filemenu leads to infinite loop regress. 
On 3.4.1 with tcl/tk 8.6, this does not crash, but it might on an 
earlier version of Python and tcl/tk.

Since menubar is left empty, it is not displayed.  Fix both problems with

menubar.add_cascade(label='TK UI Sample', menu=filemenu)



> root.config(menu=menubar)
>
>
>
> #loop to listen for events
> root.mainloop()
>


-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#75766

FromNicholas Cannon <nicholascannon1@gmail.com>
Date2014-08-05 15:28 -0700
Message-ID<c2e3b6d8-da9a-447d-b67e-01877159412d@googlegroups.com>
In reply to#75760
Ok so I am on 2.7.8.
> What x.y.z version of Python. How did you run it, exactly?

> Adding filemenu as a submenu of filemenu leads to infinite loop regress. 
> 
> On 3.4.1 with tcl/tk 8.6, this does not crash, but it might on an 
> 
> earlier version of Python and tcl/tk.
> Since menubar is left empty, it is not displayed.  Fix both problems with
> 
>menubar.add_cascade(label='TK UI Sample', menu=filemenu)
> 
> root.config(menu=menubar)
Yeah this fixed the problem. So the main menu object needs to be cascade instead of the filemenu. Will this need to be done every I create a new menu?


>and ran -- no crash, no error message, no menu. I entered text into box, 
>clicked Submit text, and OK on popup, and nothing happens. 
Im not quite sure what is happening here. Oh I just looked at the code and the part that sends the entry box text is in the wrong place or must have been unindented I have fixed this now and it works great.

[toc] | [prev] | [next] | [standalone]


#75769

FromTerry Reedy <tjreedy@udel.edu>
Date2014-08-05 19:27 -0400
Message-ID<mailman.12681.1407281280.18130.python-list@python.org>
In reply to#75766
On 8/5/2014 6:28 PM, Nicholas Cannon wrote:
> Ok so I am on 2.7.8.
>> What x.y.z version of Python. How did you run it, exactly?
>
>> Adding filemenu as a submenu of filemenu leads to infinite loop regress.
>>
>> On 3.4.1 with tcl/tk 8.6, this does not crash, but it might on an
>>
>> earlier version of Python and tcl/tk.
>> Since menubar is left empty, it is not displayed.  Fix both problems with
>>
>> menubar.add_cascade(label='TK UI Sample', menu=filemenu)
>>
>> root.config(menu=menubar)
> Yeah this fixed the problem. So the main menu object needs to be cascade instead of the filemenu. Will this need to be done every I create a new menu?

I am not sure what you mean here. The main menu bar menubar is not a 
cascade. Added to root as the menu attribute, it displays horizonatally. 
You add filemenu to menebar as a cascade. It then displays vertically 
under its label on the main menu. It is fairly conventional that all the 
entries on the main menu are cascades, but apparently not necessary. It 
is also fairly conventional that most items on drop down menus are not 
cascades, but you could add a third menu to filemenu as a cascade.

>> and ran -- no crash, no error message, no menu. I entered text into box,
>> clicked Submit text, and OK on popup, and nothing happens.
> Im not quite sure what is happening here. Oh I just looked at the code and the part that sends the entry box text is in the wrong place or must have been unindented I have fixed this now and it works great.
>


-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#75771

FromNicholas Cannon <nicholascannon1@gmail.com>
Date2014-08-05 16:33 -0700
Message-ID<98374ac7-7777-4963-a9a9-3fd70803ea74@googlegroups.com>
In reply to#75769
I am confused. When I did menu bar.add_cascade why don't I do filemenu.add_cascade. Is it because I am adding a cascade to the main menubar?  

[toc] | [prev] | [next] | [standalone]


#75775

FromTerry Reedy <tjreedy@udel.edu>
Date2014-08-06 00:13 -0400
Message-ID<mailman.12685.1407298465.18130.python-list@python.org>
In reply to#75771
On 8/5/2014 7:33 PM, Nicholas Cannon wrote:
> I am confused. When I did menu bar.add_cascade why don't I do filemenu.add_cascade. Is it because I am adding a cascade to the main menubar?

Let us start with a widget, that can 'contain' other widgets (and 
possibly other things). We create a child widget (which keeps a 
reference to the parent. Now we want to put it into the parent. How?

The generic grid, pack, and place geometry methods are called on the 
child, with no mention of the parent. (The child reference to the parent 
is used instead.)  Do note, however, that child-independent geometry 
configure methods, like grid rowconfigure, are called on the parent.

Widget-specific geometry methods, however, are (mostly at least) called 
on the parent, with the child passed as a parameter.  Menus can contain 
both commands, which are not widgets, and submenus, which are. They are 
packed with add_command and add_cascade.  Canvases have add methods that 
place items, which again may or may not be widgets.

I initially found child.pack(args) confusing, because I expected the 
pattern to be (child.parent).pack(child, args).  But now that I think 
about it, the shortcut is similar to instance.method(args) sometimes 
meaning (instance.__class__).method(instance, args).  The convenience in 
both cases is about the same.

-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web