From bogus at bogzab.plus.com Mon Nov 2 22:45:43 2009 From: bogus at bogzab.plus.com (bogzab) Date: Mon, 2 Nov 2009 13:45:43 -0800 (PST) Subject: [Tkinter-discuss] How to make Button Command work as a called function ? Message-ID: <26157856.post@talk.nabble.com> I think I am making a rookie error with this somewhere but cannot figure out where. If I create a button with these lines of code : topwin = Tk() ... frame = Frame(topwin) ... butt2 = Button(frame, text="Not yet", command=topwin.iconfiy) When the program runs, the button acts as expected and the window is iconified. If however the line which creates the button is : butt2 = Button(frame, text="Not yet", command=wait_a_bit) where wait_a_bit is a function I define as follows : def wait_a_bit(): topwin.iconify print "Pressed No" then the print statement in this function works, but the topwin.iconify statement does not. I have tried declaring topwin to be global but this made no difference. Can anybody point me to where my error lies in the second approach ? I want to do it as a function because there are other bits of code as well as the iconify that I need to incorporate. -- View this message in context: http://old.nabble.com/How-to-make-Button-Command-work-as-a-called-function---tp26157856p26157856.html Sent from the Python - tkinter-discuss mailing list archive at Nabble.com. From bogus at bogzab.plus.com Mon Nov 2 22:48:53 2009 From: bogus at bogzab.plus.com (bogzab) Date: Mon, 2 Nov 2009 13:48:53 -0800 (PST) Subject: [Tkinter-discuss] How to make Button Command work as a called function ? Message-ID: <26157859.post@talk.nabble.com> I think I am making a rookie error with this somewhere but cannot figure out where. If I create a button with these lines of code : topwin = Tk() ... frame = Frame(topwin) ... butt2 = Button(frame, text="Not yet", command=topwin.iconfiy) When the program runs, the button acts as expected and the window is iconified. If however the line which creates the button is : butt2 = Button(frame, text="Not yet", command=wait_a_bit) where wait_a_bit is a function I define as follows : def wait_a_bit(): topwin.iconify print "Pressed No" then the print statement in this function works, but the topwin.iconify statement does not. I have tried declaring topwin to be global but this made no difference. Can anybody point me to where my error lies in the second approach ? I want to do it as a function because there are other bits of code as well as the iconify that I need to incorporate. -- View this message in context: http://old.nabble.com/How-to-make-Button-Command-work-as-a-called-function---tp26157859p26157859.html Sent from the Python - tkinter-discuss mailing list archive at Nabble.com. From michael.odonnell at uam.es Tue Nov 3 09:10:08 2009 From: michael.odonnell at uam.es (Michael O'Donnell) Date: Tue, 3 Nov 2009 09:10:08 +0100 Subject: [Tkinter-discuss] How to make Button Command work as a called function ? In-Reply-To: <26157859.post@talk.nabble.com> References: <26157859.post@talk.nabble.com> Message-ID: <47e491110911030010i1f80beefne188185955361448@mail.gmail.com> HI Bogzab, > def wait_a_bit(): > topwin.iconify > print "Pressed No" change the second line for: topwin.iconify() You need to end a method call with () to indicate it is an invocation. Otherwise, the statement just returns a pointer to the method. Mick On Mon, Nov 2, 2009 at 10:48 PM, bogzab wrote: > > I think I am making a rookie error with this somewhere but cannot figure out > where. > > If I create a button with these lines of code : > > ? ? ? ?topwin = Tk() > ? ? ? ?... > ? ? ? ?frame = Frame(topwin) > ? ? ? ?... > ? ? ? ?butt2 = Button(frame, text="Not yet", command=topwin.iconfiy) > > When the program runs, the button acts as expected and the window is > iconified. > > If however the line which creates the button is : > > ? ? ? ?butt2 = Button(frame, text="Not yet", command=wait_a_bit) > > where wait_a_bit is a function I define as follows : > > ? ? ? ?def wait_a_bit(): > ? ? ? ? ? ? ? ?topwin.iconify > ? ? ? ? ? ? ? ?print "Pressed No" > > then the print statement in this function works, but the topwin.iconify > statement > does not. > > I have tried declaring topwin to be global but this made no difference. > > Can anybody point me to where my error lies in the second approach ? I want > to > do it as a function because there are other bits of code as well as the > iconify > that I need to incorporate. > > -- > View this message in context: http://old.nabble.com/How-to-make-Button-Command-work-as-a-called-function---tp26157859p26157859.html > Sent from the Python - tkinter-discuss mailing list archive at Nabble.com. > > _______________________________________________ > Tkinter-discuss mailing list > Tkinter-discuss at python.org > http://mail.python.org/mailman/listinfo/tkinter-discuss > From bogus at bogzab.plus.com Tue Nov 3 20:20:18 2009 From: bogus at bogzab.plus.com (Bogus Zaba) Date: Tue, 03 Nov 2009 19:20:18 +0000 Subject: [Tkinter-discuss] How to make Button Command work as a called function ? In-Reply-To: <47e491110911030010i1f80beefne188185955361448@mail.gmail.com> References: <26157859.post@talk.nabble.com> <47e491110911030010i1f80beefne188185955361448@mail.gmail.com> Message-ID: <4AF08272.7020705@bogzab.plus.com> I was right - a rookie mistake. Sorry to take up your time... Bogus Michael O'Donnell wrote: > HI Bogzab, > > >> def wait_a_bit(): >> topwin.iconify >> print "Pressed No" >> > > change the second line for: > topwin.iconify() > > You need to end a method call with () to indicate it is > an invocation. Otherwise, the statement just returns > a pointer to the method. > > Mick > > > On Mon, Nov 2, 2009 at 10:48 PM, bogzab wrote: > >> I think I am making a rookie error with this somewhere but cannot figure out >> where. >> >> If I create a button with these lines of code : >> >> topwin = Tk() >> ... >> frame = Frame(topwin) >> ... >> butt2 = Button(frame, text="Not yet", command=topwin.iconfiy) >> >> When the program runs, the button acts as expected and the window is >> iconified. >> >> If however the line which creates the button is : >> >> butt2 = Button(frame, text="Not yet", command=wait_a_bit) >> >> where wait_a_bit is a function I define as follows : >> >> def wait_a_bit(): >> topwin.iconify >> print "Pressed No" >> >> then the print statement in this function works, but the topwin.iconify >> statement >> does not. >> >> I have tried declaring topwin to be global but this made no difference. >> >> Can anybody point me to where my error lies in the second approach ? I want >> to >> do it as a function because there are other bits of code as well as the >> iconify >> that I need to incorporate. >> >> -- >> View this message in context: http://old.nabble.com/How-to-make-Button-Command-work-as-a-called-function---tp26157859p26157859.html >> Sent from the Python - tkinter-discuss mailing list archive at Nabble.com. >> >> _______________________________________________ >> Tkinter-discuss mailing list >> Tkinter-discuss at python.org >> http://mail.python.org/mailman/listinfo/tkinter-discuss >> >> > > From michael.odonnell at uam.es Tue Nov 3 22:55:48 2009 From: michael.odonnell at uam.es (Michael O'Donnell) Date: Tue, 3 Nov 2009 22:55:48 +0100 Subject: [Tkinter-discuss] How to make Button Command work as a called function ? In-Reply-To: <4AF08272.7020705@bogzab.plus.com> References: <26157859.post@talk.nabble.com> <47e491110911030010i1f80beefne188185955361448@mail.gmail.com> <4AF08272.7020705@bogzab.plus.com> Message-ID: <47e491110911031355v663c3d24hbe21104df0cfdb71@mail.gmail.com> I have been programming python for 4 years, (and programming for 30 years) and have made your mistake on more than one occasion. Mick On Tue, Nov 3, 2009 at 8:20 PM, Bogus Zaba wrote: > I was right - a rookie mistake. Sorry to take up your time... > > Bogus > > Michael O'Donnell wrote: >> >> HI Bogzab, >> >> >>> >>> ? ? ? def wait_a_bit(): >>> ? ? ? ? ? ? ? topwin.iconify >>> ? ? ? ? ? ? ? print "Pressed No" >>> >> >> change the second line for: >> ? ?topwin.iconify() >> >> You need to end a method call with () to indicate it is >> an invocation. Otherwise, the statement just returns >> a pointer to the method. >> >> Mick >> >> >> On Mon, Nov 2, 2009 at 10:48 PM, bogzab wrote: >> >>> >>> I think I am making a rookie error with this somewhere but cannot figure >>> out >>> where. >>> >>> If I create a button with these lines of code : >>> >>> ? ? ? topwin = Tk() >>> ? ? ? ... >>> ? ? ? frame = Frame(topwin) >>> ? ? ? ... >>> ? ? ? butt2 = Button(frame, text="Not yet", command=topwin.iconfiy) >>> >>> When the program runs, the button acts as expected and the window is >>> iconified. >>> >>> If however the line which creates the button is : >>> >>> ? ? ? butt2 = Button(frame, text="Not yet", command=wait_a_bit) >>> >>> where wait_a_bit is a function I define as follows : >>> >>> ? ? ? def wait_a_bit(): >>> ? ? ? ? ? ? ? topwin.iconify >>> ? ? ? ? ? ? ? print "Pressed No" >>> >>> then the print statement in this function works, but the topwin.iconify >>> statement >>> does not. >>> >>> I have tried declaring topwin to be global but this made no difference. >>> >>> Can anybody point me to where my error lies in the second approach ? I >>> want >>> to >>> do it as a function because there are other bits of code as well as the >>> iconify >>> that I need to incorporate. >>> >>> -- >>> View this message in context: >>> http://old.nabble.com/How-to-make-Button-Command-work-as-a-called-function---tp26157859p26157859.html >>> Sent from the Python - tkinter-discuss mailing list archive at >>> Nabble.com. >>> >>> _______________________________________________ >>> Tkinter-discuss mailing list >>> Tkinter-discuss at python.org >>> http://mail.python.org/mailman/listinfo/tkinter-discuss >>> >>> >> >> > > _______________________________________________ > Tkinter-discuss mailing list > Tkinter-discuss at python.org > http://mail.python.org/mailman/listinfo/tkinter-discuss > From supersimha at gmail.com Wed Nov 4 13:18:49 2009 From: supersimha at gmail.com (simha) Date: Wed, 4 Nov 2009 04:18:49 -0800 (PST) Subject: [Tkinter-discuss] How to set option Message-ID: <26195281.post@talk.nabble.com> -- View this message in context: http://old.nabble.com/How-to-set-option-tp26195281p26195281.html Sent from the Python - tkinter-discuss mailing list archive at Nabble.com. From supersimha at gmail.com Wed Nov 4 13:20:44 2009 From: supersimha at gmail.com (simha) Date: Wed, 4 Nov 2009 04:20:44 -0800 (PST) Subject: [Tkinter-discuss] How to set optionmenu without setting the variable Message-ID: <26195310.post@talk.nabble.com> The conventional way goes this var = Stringvar() var.set('value') But i want to happen without using the variable. Can someone help me out on this? -- View this message in context: http://old.nabble.com/How-to-set-optionmenu-without-setting-the-variable-tp26195310p26195310.html Sent from the Python - tkinter-discuss mailing list archive at Nabble.com. From david.giesen at kodak.com Wed Nov 4 14:23:39 2009 From: david.giesen at kodak.com (david.giesen at kodak.com) Date: Wed, 4 Nov 2009 08:23:39 -0500 Subject: [Tkinter-discuss] How to set optionmenu without setting the variable Message-ID: I don't believe there is any way to create an OptionMenu without using a Tk variable. However, if all you want to do is to change the value of an already created OptionMenu without using the variable, you can do this: import Tkinter as Tk root = Tk.Tk() var = Tk.StringVar() om = Tk.OptionMenu(root, var, 'a', 'b', 'c') om['menu'].invoke(index) where index is the index of the value you want to select. In this case, index=0 for 'a', 1 for 'b', or 2 for 'c'. I'm not sure that's really an improvement for you, but it works. Also note - you can add additional options after creation to the OptionMenu through a bit of a backdoor using a private Tk function (so this isn't strictly recommended, I guess) using the following syntax: om['menu'].add_command(label='new_value', command=Tk._setit(var, 'new_value')) Dave > The conventional way goes this > var = Stringvar() > var.set('value') > But i want to happen without using the variable. Can someone help me out on > this? David J. Giesen | Research Scientist | FPEG US Display OLED Materials R+D | Eastman Kodak Company | 2/83/KRL MC02216 | Rochester, NY 14650 | david.giesen at kodak.com | 1-585-588-0480 Office | www.kodak.com -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/gif Size: 826 bytes Desc: not available URL: From dkgiesen at frontiernet.net Wed Nov 4 05:01:53 2009 From: dkgiesen at frontiernet.net (Dave and Kerrie Giesen) Date: Tue, 03 Nov 2009 23:01:53 -0500 Subject: [Tkinter-discuss] How to export the image drawn by canvas? Message-ID: <4AF0FCB1.8070603@frontiernet.net> Back in October, Shengu wrote: > Dears,: > > I use canvas to make animation by continuously displaying objects. > Now I want to export what is displayed in canvas to an image file(.bmp, jpeg,png,... is OK) > Is there such function in tkinter and how can I attain this? > > Thank you very much! > > Shengxu I thought I'd pass along a library that I've cobbled together over time that provides functions for copying the contents of any widget - including a canvas - to either the clipboard or an image file. To use the library, you will need the Python Imaging Library: http://www.pythonware.com/products/pil/ To use the copy to and from clipboard functions you will need wx. The library is intended to work with Tkinter widgets, but uses some wx functionality to make the clipboard functions work properly. If you don't want the clipboard functionality, you can skip the wx download and comment out the import wx and PySimpleApp lines. http://www.wxpython.org/ The library works only on Windows and includes four basic functions: grab_image: grabs an screencap of either a widget or an area of the screen and returns a PIL image object copy_to_clipboard: grabs a screencap of either a widget or an area and copies the image to the clipboard copy_to_file: grabs a screencap of either a widget or an area and writes the image to an image file such as jpeg, gif, etc. - whatever types PIL supports get_bitmap_from_clipboard: grabs a bitmap from the clipboard and returns a PIL image object I'd like to thank Fredrik Lundh for many years ago providing the code that does the screen cap of the images. Hopefully this will be helpful to people - Dave Giesen import sys import tkMessageBox as tkm import Image # Only if you need and use the PIL library. if sys.platform.startswith('win'): # Image grab comes from PIL (Python Imaging Library) import ImageGrab # We use wx to copy to the clipboard on windows machines. The PySimpleApp line initializes wx. There does not appear to be # a conflict between wx started this way and Tk. Note that if you don't want the copy to clipboard or read from clipboard capability, # you can just delete the two lines below (and all the associated wx routines). import wx app = wx.PySimpleApp() def grab_image(widget=None, bbox=None, offset1=2, offset2=2): # widget = the Tk widget to copy to the clipboard # bbox = the bounding box containing the area to copy to the clipboard. Should be a tuple of 4 integers. # either widget or bbox must not be None. If both are not None, widget is used. # Note that if widget is used, the screen will be updated. It can't do that if only bbox is used, so that should be done by the caller # offset1 & offset2 determine the amount to expand the box beyond the widget in order to make sure the whole widget is captured # they are used because some widgets (such as canvases) appear to be a bit wonky in whether borders are copied or not im = "" if widget: # The guts of this routine were originally provided by Fredrik Lundh widget.update() x0 = widget.winfo_rootx() y0 = widget.winfo_rooty() x1 = x0 + widget.winfo_width() y1 = y0 + widget.winfo_height() im = ImageGrab.grab((x0-offset1, y0-offset1, x1+offset2, y1+offset2)) elif bbox: im = ImageGrab.grab(bbox) return im def copy_to_clipboard(**keyw): # Copies an image of a widget or screen area to the clipboard. # # Any arguments to this routine are passed directly to grab_image, so see that routine for possible useful arguments # In particular, either widget or bbox must be supplied by the user # if sys.platform.startswith('win'): im = grab_image(**keyw) if im: bitmap = pil_to_bitmap(im) clipboard_bitmap = wx.BitmapDataObject(bitmap) if wx.TheClipboard.Open(): wx.TheClipboard.SetData(clipboard_bitmap) wx.TheClipboard.Close() else: tkm.showerror('Clipboard Copy Error', 'Clipboard copy not implemented on this platform') def copy_to_file(filename, **keyw): # Copies an image of a widget or screen area to a file. The file type can be any that the Python Imaging Library supports, and the # file type is determined by the extension of filename. # # filename = name of file to save the image to. The file name extension is used to determine the image type. # # Any arguments to this routine are passed directly to grab_image, so see that routine for possible useful arguments # In particular, either widget= or bbox= must be supplied by the user # if sys.platform.startswith('win'): im = grab_image(**keyw) if im: im.save(filename) else: tkm.showerror('Image Grab Error', 'Image grab not implemented on this platform') def get_bitmap_from_clipboard(): # Returns a PIL image from the clipboard, which should have bitmap data in it # The PIL image can be used in Tk widgets by converting it using ImageTk.PhotoImage(PIL image) bm = wx.BitmapDataObject() if wx.TheClipboard.GetData(bm): wxbmp = bm.GetBitmap() try: pilimage = bitmap_to_pil(wxbmp) return pilimage except: tkm.showerror('Clipboard Paste Error', 'Clipboard bitmap data could not be converted to an image') return False else: tkm.showerror('Clipboard Paste Error', 'Clipboard data is not a recognized bitmap format') return False ######################################## # Start of routines taken from http://wiki.wxpython.org/index.cgi/WorkingWithImages ######################################## def bitmap_to_pil(bitmap): return wximage_to_pil(bitmap_to_wximage(bitmap)) def bitmap_to_wximage(bitmap): return wx.ImageFromBitmap(bitmap) def pil_to_bitmap(pil): return wximage_to_bitmap(pil_to_wximage(pil)) def pil_to_wximage(pil): image = wx.EmptyImage(pil.size[0], pil.size[1]) image.SetData(pil.convert('RGB').tostring()) return image #Or, if you want to copy alpha channels too (available from wxPython 2.5) def piltoimage(pil,alpha=True): if alpha: image = apply( wx.EmptyImage, pil.size ) image.SetData( pil.convert( "RGB").tostring() ) image.SetAlphaData(pil.convert("RGBA").tostring()[3::4]) else: image = wx.EmptyImage(pil.size[0], pil.size[1]) new_image = pil.convert('RGB') data = new_image.tostring() image.SetData(data) return image def wximage_to_pil(image): pil = Image.new('RGB', (image.GetWidth(), image.GetHeight())) pil.fromstring(image.GetData()) return pil def wximage_to_bitmap(image): return image.ConvertToBitmap() ######################################## # End of routines taken from http://wiki.wxpython.org/index.cgi/WorkingWithImages ######################################## if __name__ == '__main__': def copy_canvas(): copy_to_clipboard(widget=canvas) def copy_canvas_file(): copy_to_file('testimage.jpg', widget=canvas) print 'testing' import Tkinter as Tk root = Tk.Tk() canvas = Tk.Canvas(root, bg='white') canvas.pack() canvas.create_oval(150, 50, 180, 150, fill='red') canvas.create_line(3, 3, 100, 120, fill='blue', width=4) button1 = Tk.Button(root, text='copy to clipboard', command=copy_canvas) button1.pack(pady=5) button2 = Tk.Button(root, text='copy to testimage.jpg', command=copy_canvas_file) button2.pack() root.mainloop() From klappnase at web.de Wed Nov 4 20:30:38 2009 From: klappnase at web.de (Michael Lange) Date: Wed, 4 Nov 2009 20:30:38 +0100 Subject: [Tkinter-discuss] How to set optionmenu without setting the variable In-Reply-To: References: Message-ID: <20091104203038.89f437c8.klappnase@web.de> Hi, On Wed, 4 Nov 2009 08:23:39 -0500 david.giesen at kodak.com wrote: > I don't believe there is any way to create an OptionMenu without > using a Tk variable. Actually you can: from Tkinter import * root = Tk() om = OptionMenu(root, None, '1', '2') om.pack(padx=100, pady=100) items = ('a', 'b', 'c', 'd') def callback(item): om.configure(text=item) print 'selected:', item om['menu'].delete(0, 'end') for x in items: om['menu'].add_command(label=x, command=lambda item=x: callback(item)) callback('a') root.mainloop() I needed this once, because I was not able to handle unicode filenames properly using a Tk textvariable, so you see it looks overly complicated but in some cases may have its use. I hope this helps Michael From david.giesen at kodak.com Wed Nov 4 20:44:10 2009 From: david.giesen at kodak.com (david.giesen at kodak.com) Date: Wed, 4 Nov 2009 14:44:10 -0500 Subject: [Tkinter-discuss] How to set optionmenu without setting the variable In-Reply-To: <20091104203038.89f437c8.klappnase@web.de> References: <20091104203038.89f437c8.klappnase@web.de> Message-ID: Well - now I can say I learned something today! Your method also demonstrates how to add additional items after creation without using the Tkinter private routine _setit, which is also nice. Dave David J. Giesen | Research Scientist | FPEG US Display OLED Materials R+D | Eastman Kodak Company | 2/83/KRL MC02216 | Rochester, NY 14650 | david.giesen at kodak.com | 1-585-588-0480 Office | www.kodak.com From: Michael Lange To: tkinter-discuss at python.org Date: 11/04/2009 02:30 PM Subject: Re: [Tkinter-discuss] How to set optionmenu without setting the variable Sent by: tkinter-discuss-bounces+david.giesen=kodak.com at python.org Hi, On Wed, 4 Nov 2009 08:23:39 -0500 david.giesen at kodak.com wrote: > I don't believe there is any way to create an OptionMenu without > using a Tk variable. Actually you can: from Tkinter import * root = Tk() om = OptionMenu(root, None, '1', '2') om.pack(padx=100, pady=100) items = ('a', 'b', 'c', 'd') def callback(item): om.configure(text=item) print 'selected:', item om['menu'].delete(0, 'end') for x in items: om['menu'].add_command(label=x, command=lambda item=x: callback(item)) callback('a') root.mainloop() I needed this once, because I was not able to handle unicode filenames properly using a Tk textvariable, so you see it looks overly complicated but in some cases may have its use. I hope this helps Michael _______________________________________________ Tkinter-discuss mailing list Tkinter-discuss at python.org http://mail.python.org/mailman/listinfo/tkinter-discuss -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/gif Size: 826 bytes Desc: not available URL: From supersimha at gmail.com Mon Nov 9 11:01:07 2009 From: supersimha at gmail.com (simha) Date: Mon, 9 Nov 2009 02:01:07 -0800 (PST) Subject: [Tkinter-discuss] How to set option menu without setting variable Message-ID: <26263738.post@talk.nabble.com> My issue is as follows... I have a dictionary which I use for display. I have an object which has 4 optionmenu and 4 text boxes. this object is displayed when i click on tree node.(display value depends on dictionary) If i have 4 tree nodes, object remains same but dictionary changes. So if at one tree node I set the variable to say 'x' for an optionmenu, then it is getting projected in other 3 objects.. How do I fix this? It will be great help. Thanks in advance -- View this message in context: http://old.nabble.com/How-to-set-option-menu-without-setting-variable-tp26263738p26263738.html Sent from the Python - tkinter-discuss mailing list archive at Nabble.com.