Three questions about Tkinter

Eric Brunel eric.brunel at pragmadev.com
Wed Apr 30 10:43:59 EDT 2003


Arnal wrote:
> Hi,
> 
> I'm new at python programming (as well as english writing!), and I'm trying
> to make a simple text editor.
> 
> I'm using Tkinter, but I've the following problems:
> 
> -I can't figure out how to use a printer to print under Windows.

Short answer: you can't. That's one of the biggest issues in Tkinter and tk 
(IMHO...): there is no access to the printing facilities on the current 
platform, whatever that means. Note it's a limitation of tk itself, not of 
Tkinter. I hope the tcl/tk guys will take care of this issue some day.

You can access the regular Windows printing facilities via the win32ui et al 
modules, but it's far from easy and it may even crash your application on Win9x. 
The usual workaround is to generate in a "common" format like .doc or .pdf, then 
   print the generated file via an external application.

> -I use a "text" control (I dislike the "widget" term, sorry), but I can't

(NB: you really should learn to like the "widget" term, since it's quite the 
standard name for these things... :-) )

> resize it on the fly (the width and height properties are measured in
> characters and lines, not in pixels). My current workaround is to make the
> window not resizable but it would be nice to do it.

I don't exactly understand what you're trying to do here. What's the problem? 
Can you post a piece of code showing it?

> -I've a sub menu for recents files. When I read in my pref file to list
> every file paths stored in it, I would like to add them in the recents
> submenu. I can't find how to do it. I tried, inside a "while" with the
> variable i:
> 
> MRecents.insert_command(0,label=GetFileName(file),accelerator=acc,command=Op
> enRecent(i))
> 
> MRecents.insert_command(0,label=GetFileName(file),accelerator=acc,command=Op
> enRecent i)
> 
> (MRecents is a global variable representing my submenu, GetFileName is a
> function, no special meaning in the problem,acc is defined). In the first
> example, the OpenRecent function is called directly, that's the normal
> behavior, and there is a syntax error in the second example.
> How to define an array of menu items with all calling the same function with
> a parameter?

The most common way to do it is via a lambda function. If you don't know what it 
is, look at:
http://www.python.org/doc/current/tut/node6.html#SECTION006740000000000000000

Practically, you'd write what you want to do like follows:

MRecents.insert_command(0, label=GetFileName(file),
                            accelerator=acc,
                            command=lambda i=i: OpenRecent(i))

There are more sophisticated solutions but this one should be enough for you.

HTH
-- 
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com





More information about the Python-list mailing list