Protosssword | 1 Oct 02:42 2009
Picon

How to export the image drawn by canvas?

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

立刻下载 MSN 保护盾,保障Messenger 安全稳定! 现在就下载!
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss <at> python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
Michael Lange | 1 Oct 12:39 2009
Picon

Re: How to export the image drawn by canvas?

Hi,

On Thu, 1 Oct 2009 08:42:48 +0800
Protosssword <xsxcn <at> hotmail.com> 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?
> 

You can use the canvases postscript() method to export the whole canvas
or a part of it to a postscript file or alternatively let postscript()
return the postscript data. If necessary, you can use PIL to convert
the postscript into png or jpeg or so.

I hope this helps

Michael
Lion Kimbro | 1 Oct 04:17 2009
Picon

Re: How to export the image drawn by canvas?

 
  I don't believe there's a function.
 
  I've read about people making functions that save a postscript [1], and open a command shell to instruct ImageMagick to conver it, and I've read about people using software that bridges tkinter with PIL [2].
 
  As for storing animations, I've never heard of people doing that with tkinter.
 
  Related wiki articles:
 

  [1]  tkinter.Canvas.postscript:
  [2]  the ImageTk module
 
 
 
2009/9/30 Protosssword <xsxcn <at> hotmail.com>
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

立刻下载 MSN 保护盾,保障Messenger 安全稳定! 现在就下载!

_______________________________________________
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
Nessuss | 3 Oct 07:00 2009
Picon

Tkinter on OS X wont import


Hi,

I've been trying to work with some python programs that use Tkinter in them
but when python tries to import the Tkinter package I get this error:

<code>
import Tkinter
  File
"/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk/Tkinter.py",
line 38, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError:
dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload/_tkinter.so,
2): Library not loaded: /opt/local/lib/libX11.6.dylib
  Referenced from:
/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload/_tkinter.so
  Reason: no suitable image found.  Did find:
        /opt/local/lib/libX11.6.dylib: mach-o, but wrong architecture
</code>

I can't seem to figure out the problem, some time ago I installed using
macports tcl and tk, everything was working then. But now it gives the above
error. In between the time Tkinter was last working and now I did upgrade to
Snow Leopard. If anyone can help me out here, that would be great.
--

-- 
View this message in context: http://www.nabble.com/Tkinter-on-OS-X-wont-import-tp25725811p25725811.html
Sent from the Python - tkinter-discuss mailing list archive at Nabble.com.
Kevin Walzer | 7 Oct 16:06 2009

Execute Python callback from Tk widgets

Hi all,

I'm running into some difficulty in wrapping a Tk widget from Python in 
a way that will allow me to execute Python callbacks/commands.

I'm trying to incorporate widgets from TclMacBag 
(http://tclmacbag.autons.net/) into one of my Tkinter programs. 
Specifically, I'm trying to use the "stylebutton" widget.  At their 
base, the TclMacBag stylebuttons are just ttk::buttons with a custom 
style. However, they are constructed as megawidgets, their internal API 
is quite complex, and so getting at their underlying button and style 
implementation from Tkinter is difficult.

My current approach is to define a simple Tcl/Tk package that exposes 
the functionality I need, and then use self.tk.call() to access this 
from Tkinter. Here's an abbreviated example:

##Tcl code in "macstylebutton" package
proc macstylebutton {w child img cmd layout msg row column } {

     ::tclmacbag::stylebutton $w.$child -image $img -style $layout 
-command $cmd
     grid $w.$child -row $row -column $column
     balloon $w.$child $msg
}

#Python function
    def styleButton(self, parent, child, img, cmd, style, msg, row, column):
         self.tk.call('package', 'require', 'macstylebutton')
         self.tk.call('macstylebutton', parent, child, img, cmd, style, 
msg, row, column)

#Python GUI code that calls the function
        self.deletebutton=self.styleButton(self.topframe, 'delete', 
self.logout, lambda: self.authorizeCommand(self.deletePackage), 'mail', 
'Delete package', 0, 2)

This code lays the buttons out correctly, but the Python callback is not 
executed. It seems to be correctly set up; running "print cmd"  yields 
output like this:

<bound method phynchronicityApp.infoPackage of 
<__main__.phynchronicityApp instance at 0x3f1af8>>

but the actual callback doesn't fire.

My theory as to the problem is this: it appears that the widget is 
responding to the "cmd" parameter as a Tcl command, and since it's 
actually a Python command, nothing's happening. I know Python/Tkinter 
can execute Tcl commands through 'call' or 'eval,' but it doesn't appear 
that Python commands can be passed to raw Tk widgets. Indeed, I had 
tried to simply configure the buttons via something like 
'self.deleteButton.configure(command=foo'), but Tkinter returned an 
error--something about a NoneType object lacking a 'configure' method.

My question is this: how can I get Python callbacks to fire when a raw 
Tk widget is being called via 'self.tk.call'? Do I have to write a fancy 
wrapper that turns the widget into a Python object that will respond to 
Python callbacks, or is there a simpler way to do this?

Thanks,
Kevin

--

-- 
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
Russell E. Owen | 7 Oct 21:00 2009

Re: Execute Python callback from Tk widgets

In article <4ACCA070.3050603 <at> codebykevin.com>,
 Kevin Walzer <kw <at> codebykevin.com> wrote:

>...
> My question is this: how can I get Python callbacks to fire when a raw 
> Tk widget is being called via 'self.tk.call'? Do I have to write a fancy 
> wrapper that turns the widget into a Python object that will respond to 
> Python callbacks, or is there a simpler way to do this?

I use this class (from the RO package's module RO.TkUtil):

class TclFunc:
    """Register a python function as a tcl function.
    Based on Tkinter's _register method (which, being private,
    I prefer not to use explicitly).

    If the function call fails, a traceback is printed.

    Please call deregister when you no longer
    want the tcl function to exist.
    """
    tkApp = None
    def __init__(self, func, debug=False):
        if self.tkApp == None:
            self.tkApp =Tkinter.Frame().tk
        self.func = func
        self.tclFuncName = "pyfunc%s" % (id(self),)
        self.debug = bool(debug)
        try:
            self.tclFuncName += str(func.__name__)
        except AttributeError:
            pass
        if self.debug:
            print "registering tcl function %s for python function %s" % 
(self.tclFuncName, func)
        self.tkApp.createcommand(self.tclFuncName, self)

    def __call__(self, *args):
        try:
            self.func(*args)
        except (SystemExit, KeyboardInterrupt):
            raise
        except Exception, e:
            sys.stderr.write("tcl function %s failed: %s\n" % 
(self.tclFuncName, e))
            traceback.print_exc(file=sys.stderr)

    def deregister(self):
        """Deregister callback and delete reference to python function.
        Safe to call if already deregistered.
        """
        if self.debug:
            print "%r.deregister()" % (self,)
        if not self.func:
            if self.debug:
                print "already deregistered"
            return
        try:
            self.tkApp.deletecommand(self.tclFuncName)
        except Tkinter.TclError, e:
            if self.debug:
                print "deregistering failed: %r" % (e,)
            pass
        self.func = None

    def __repr__(self):
        return "%s(%s)" % (self.__class__.__name__, self.tclFuncName)

    def __str__(self):
        return self.tclFuncName
Kevin Walzer | 8 Oct 01:17 2009

Re: Execute Python callback from Tk widgets

Hi Russell,

On 10/7/09 3:00 PM, Russell E. Owen wrote:

>
> I use this class (from the RO package's module RO.TkUtil):
>
> class TclFunc:
>      """Register a python function as a tcl function.
>      Based on Tkinter's _register method (which, being private,
>      I prefer not to use explicitly).

<snip>

Thanks for this reference. It led me directly to the "register" method 
in Tkinter. Now my commands are as simple as 
self.register(self.infoTable), and they work.

Best,
Kevin

--

-- 
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
BlackKnight | 14 Oct 05:14 2009
Picon

Need help on gui focus l

Hi folks,

I am using Tkinter to do a simple GUI for my program.

It contains A MainGui and A ChildGui:
A button on the MainGUI will popout the childgui.
On the childgui there is a browse button which popout a dialog to allow
user to browser and choose a file.

I have no problem at doing them.

The problem is after the uesr choose a file the focus will return to the
MainGui intead of the childgui. The child gui just go hide behind the Main.

I've tried focus_set, focus_force but none of them seem work.

I did some research but found nothing working. It's not a big problem,
but is really really annoying.
(Actually I notice the examples from some python book I am reading has
the exact same problem. The author even tried to spawn the child gui
away from the main one, so when the focus return from the browse dialog
, the maingui won't block the child one~~ Is that how tkinter should work?)

Any suggestions?

Thanks in advance~~

Frank
Michael Lange | 14 Oct 21:03 2009
Picon

Re: Need help on gui focus l

Hi,

On Tue, 13 Oct 2009 23:14:35 -0400
BlackKnight <wwwwar3com <at> gmail.com> wrote:

> Hi folks,
> 
> I am using Tkinter to do a simple GUI for my program.
> 
> It contains A MainGui and A ChildGui:
> A button on the MainGUI will popout the childgui.
> On the childgui there is a browse button which popout a dialog to
> allow user to browser and choose a file.
> 
> I have no problem at doing them.
> 
> The problem is after the uesr choose a file the focus will return to
> the MainGui intead of the childgui. The child gui just go hide behind
> the Main.
> 
> I've tried focus_set, focus_force but none of them seem work.
> 
> I did some research but found nothing working. It's not a big problem,
> but is really really annoying.
> (Actually I notice the examples from some python book I am reading has
> the exact same problem. The author even tried to spawn the child gui
> away from the main one, so when the focus return from the browse
> dialog , the maingui won't block the child one~~ Is that how tkinter
> should work?)
> 

I cannot reproduce this behavior here (linux), maybe it is a platform
specific problem ? I tried the follwing which works for me as expected:

### CODE ###
from Tkinter import *
import tkFileDialog

root = Tk()

top = Toplevel()
top.withdraw()
top.protocol('WM_DELETE_WINDOW', top.withdraw)

def filedialog():
    print tkFileDialog.askopenfilename()

b0 = Button(top, text='filedialog', command=filedialog)
b0.pack(padx=100, pady=100)

def popup():
    top.deiconify()
    b0.focus_set()

b1 = Button(root, text='popup', command=popup)
b1.pack(padx=100, pady=100)
root.mainloop()
### CODE ###

In case I just did not understand exactly your problem,
could you post a short code snippet here that shows
the problem and tell us which platform you are using, please ?

Michael
Cam Farnell | 14 Oct 22:07 2009
Picon

Re: Need help on gui focus l

The code given exhibits the problem on my Ubuntu Linux box. For the sake of clarity I enumerate three windows
involved in this code:

- The MAIN window which has a button marked "popup" in it.
- The CHILD windows which has a button marked "filedialog" in it.
- The OPEN window which is the tkFileDialog

On my machine clicking on the "popup" and "filedialog" buttons works ok, but once you select a file and click
OPEN, then what is on top and visible is the MAIN window whereas what the original poster wanted was to see
the CHILD window. Even worse, if (starting from scratch again) you click the "popup" button, then drag the
resulting CHILD window to a slightly new position then click the "filedialog" button, the OPEN window
appears *under* the CHILD window which seems less than useful.

Cam Farnell

Michael Lange wrote:

> I cannot reproduce this behavior here (linux), maybe it is a platform
> specific problem ? I tried the follwing which works for me as expected:
> 
> ### CODE ###
> from Tkinter import *
> import tkFileDialog
> 
> root = Tk()
> 
> top = Toplevel()
> top.withdraw()
> top.protocol('WM_DELETE_WINDOW', top.withdraw)
> 
> def filedialog():
>     print tkFileDialog.askopenfilename()
> 
> b0 = Button(top, text='filedialog', command=filedialog)
> b0.pack(padx=100, pady=100)
> 
> def popup():
>     top.deiconify()
>     b0.focus_set()
> 
> b1 = Button(root, text='popup', command=popup)
> b1.pack(padx=100, pady=100)
> root.mainloop()
> ### CODE ###
> 
> In case I just did not understand exactly your problem,
> could you post a short code snippet here that shows
> the problem and tell us which platform you are using, please ?
> 
> Michael

Gmane