1,105,578 Community Members

how to opening a new window on clicking menu item?

Member Avatar
katharnakh
Posting Whiz in Training
237 posts since Jan 2006
Reputation Points: 7 [?]
Q&As Helped to Solve: 34 [?]
Skill Endorsements: 0 [?]
 
0
 

how to open a new window only when i click menuitem. for example, check out this following code, in which Toplevel window comes up as soon as u run the code. i dont want that. i want the new window to open only when click 'one' in the menu bar. similarly 'second' like that.
also i want close the window when the user closes the window and again when he clicks on the 'one' all Entry fields should be empty.

can we create a form so that all Entry fields and Labels arranged in a proper manner?

please check out this code and please tell where im wrong. :?: :-|

from Tkinter import *
import tkMessageBox
import tkSimpleDialog
class form(tkSimpleDialog.Dialog):
    def __init__(self,root):
        self.menubar=Menu(root)
        root.config(menu=self.menubar)

        self.parent=root
        #self.top=top

        self.formmenu=Menu(self.menubar)
        self.formmenu.add_command(label="one",command=self.one())
        self.menubar.insert_cascade(1,label="Form",menu=self.formmenu)
        self.parent.focus_force()

    def one(self):
        tkMessageBox.showinfo("one","this first.......")
        self.top=Toplevel(master=self.parent)
        Label(self.top,text="Name: ").pack()
        e1=Entry(self.top)
        e1.pack()
        b1=Button(self.top,text="  Ok  ",command=self.ok)
        b1.pack()
        e1.focus_set()

    def ok(self):
        tkMessageBox.showinfo("one","the first form is responding to click.......")
        self.top.destroy()

root=Tk()
#top=Toplevel()
form(root)
#root.mainloop()

Edit: added code tags vegaseat

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

For starters, here is a simple code sample for bringing up a child window. You can use a button, or in your case a menu click.

# display message in a child window

from Tkinter import *

def messageWindow():
    # create child window
    win = Toplevel()
    # display message
    message = "This is the child window"
    Label(win, text=message).pack()
    # quit child window and return to root window
    # the button is optional here, simply use the corner x of the child window
    Button(win, text='OK', command=win.destroy).pack()

    
# create root window
root = Tk()
# put a button on it, or a menu
Button(root, text='Bring up Message', command=messageWindow).pack()
# start event-loop
root.mainloop()
Question Answered as of 4 Years Ago by vegaseat
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article