[Tutor] Error message with testing Tkinter
Hi, I'm learning Tkinter with the following code on Mac OS X 10.4: from Tkinter import * from sys import stdout, exit widget = Button(None, text = 'Hello?', command=(lambda: stdout.write('Hello?\n') or exit())) widget.pack() widget.mainloop() I do successfully get a GUI with a button, but the problem is if I click the button, the GUI window hangs there with the following message message in the console: Hello? Traceback (most recent call last): File "/Users/gangc/Python_Scripting/test.py", line 7, in -toplevel- widget.mainloop() File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/lib-tk/Tkinter.py", line 965, in mainloop self.tk.mainloop(n) File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/lib-tk/Tkinter.py", line 1347, in __call__ raise SystemExit, msg SystemExit What am I missing? Any help will be highly appreciated, Joe Get your own "800" number Voicemail, fax, email, and a lot more http://www.ureach.com/reg/tag ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Creating Tkinter Menubars
Hi, I am testing the following Tkinter code (attached at the end of this message) by Fredrik Lundh on a Mac OS X 10.4.2 with Python version 2.3. I do get a root window, but it is totally blank without the desirable menubars such as File and Edit. What am I missing? Any help will be highly appreciated. Thanks, Joe = from Tkinter import * class AppUI(Frame): def __init__(self, master=None): Frame.__init__(self, master, relief=SUNKEN, bd=2) self.menubar = Menu(self) menu = Menu(self.menubar, tearoff=0) self.menubar.add_cascade(label="File", menu=menu) menu.add_command(label="New") menu = Menu(self.menubar, tearoff=0) self.menubar.add_cascade(label="Edit", menu=menu) menu.add_command(label="Cut") menu.add_command(label="Copy") menu.add_command(label="Paste") try: self.master.config(menu=self.menubar) except AttributeError: # master is a toplevel window (Python 1.4/Tkinter 1.63) self.master.tk.call(master, "config", "-menu", self.menubar) self.canvas = Canvas(self, bg="white", width=400, height=400, bd=0, highlightthickness=0) self.canvas.pack() root = Tk() app = AppUI(root) app.pack() root.mainloop() Get your own "800" number Voicemail, fax, email, and a lot more http://www.ureach.com/reg/tag ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Creating Tkinter Menubars
Hi Michael, Thank you very much for the help. I tried the simpler code you provided, but unfortunatly I still got a blank window without any trace of menubars. Any other insights? Thanks, Joe from Tkinter import * root = Tk() menubar = Menu(root) menu = Menu(menubar, tearoff=0) menubar.add_cascade(label="File", menu=menu) menu.add_command(label="New") menu = Menu(menubar, tearoff=0) menubar.add_cascade(label="Edit", menu=menu) menu.add_command(label="Cut") menu.add_command(label="Copy") menu.add_command(label="Paste") root.config(menu=menubar) root.mainloop() Get your own "800" number Voicemail, fax, email, and a lot more http://www.ureach.com/reg/tag On Wed, 16 Nov 2005, Michael Lange ([EMAIL PROTECTED]) wrote: > On Tue, 15 Nov 2005 16:17:53 -0500 > Double Six <[EMAIL PROTECTED]> wrote: > > > Hi, > > > > I am testing the following Tkinter code (attached at the end of > > this message) by Fredrik Lundh on a Mac OS X 10.4.2 with Python > > version 2.3. I do get a root window, but it is totally blank > > without the desirable menubars such as File and Edit. What am I > > missing? > > > > > It works well for me (on linux, python-2.3), maybe a mac specific thing (sorry , I can't help then). > Does the following, simpler code work for you? > > from Tkinter import * > > root = Tk() > menubar = Menu(root) > menu = Menu(menubar, tearoff=0) > menubar.add_cascade(label="File", menu=menu) > menu.add_command(label="New") > menu = Menu(menubar, tearoff=0) > menubar.add_cascade(label="Edit", menu=menu) > menu.add_command(label="Cut") > menu.add_command(label="Copy") > menu.add_command(label="Paste") > root.config(menu=menubar) > root.mainloop() > > The only thing that looks a little starnge to me in the original code is > that the menubar is created as a child of the AppUi class, which is basically a Frame, > but then it is attached to that Frame's parent (the root window). > Maybe the mac doesn't like this (just a thought)? > > Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] command in menu and button
Hi all, I'm puzzled by the 'command' option in menu and Button of Tkinter. With the following lines, menu.add_command(label="Open Viewer", command=os.system("Open my viewer &")) Button(toolbar, text='Open Viewer', command=os.system("Open my viewer &")).pack(side=LEFT) I wanted to open a graphical viewer from the prompt (that is why I used os.system("Open viewer &")) once the user selects the option from the menu, or clicks the button, but this gets implements whenever the GUI is launched instead. Why is this? And how can I make it work? Many thanks, Joe Get your own "800" number Voicemail, fax, email, and a lot more http://www.ureach.com/reg/tag ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] command in menu and button
Hi John and Alan, I got it! Thank you both for explaining this situation. Thanks, Joe Get your own "800" number Voicemail, fax, email, and a lot more http://www.ureach.com/reg/tag On Tue, 22 Nov 2005, [EMAIL PROTECTED] ([EMAIL PROTECTED]) wrote: > >>I'm puzzled by the 'command' option in menu and Button of > >>Tkinter. With the following lines, > > The command value needs to be a *reference* to a function. > > That is not the function call itself but a reference to the function > that will be \called. > > Let me illustrate the difference: > > def f(): print 'Its me!' > > f() # prints the message > > g = f # this assigns a reference to f > > g() # this now calls that reference, > so calling g() is the same as calling f() > > > >>menu.add_command(label="Open Viewer", >command=os.system("Open my viewer &")) > > Here you assign the result of the os.system() > call to command, in fact you want to assign > a reference to a call of os.system which will > be executed when the menu/button is activated. > > The more straightforward way to do that is to > define a short function that calls os.system: > > def callSystem(): >os.system(Mycommand) > > And make the menu/button reference callSystem: > > >>menu.add_command(label="Open Viewer", >command=callSystem) > > Notice no parens, just the name of the function. > > Because we can wind up with loads of these little > wrapper functions there is a shortcut called lambda. > With lambda we can avoid defining a new mini function: > > >>menu.add_command(label="Open Viewer", >command=lambda : os.system("Open my viewer &")) > > the thing that follows the lambda is what gets > executed when the widget activates. > > Does that help? > > Alan G. > http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor