Three questions about Tkinter

sismex01 at hebmex.com sismex01 at hebmex.com
Wed Apr 30 15:57:01 CEST 2003


> From: Arnal [mailto:arnaud at tribu.ch]
> Sent: Wednesday, April 30, 2003 8:43 AM
> 
> Hi,
> 

howdy!

> I'm new at python programming (as well as english writing!), 
> and I'm trying to make a simple text editor.

You too?  ;-)

> 
> I'm using Tkinter, but I've the following problems:
> 
> -I can't figure out how to use a printer to print under Windows.
> 

That's a problem with Tk/Tkinter.  I don't know if Tk has a printer
output mode; I *do* know that the Canvas widget ("wee-dgit" ;-)
can generate postcript, probably describing it's current contents,
but I've never used it.

Printer output is a whole can of worms of it's own.

OTOH... if you want to simply output the contents of a text
control to the printer, minding not any fancyness, you can
send it simply as text:

...<python code>
# Open printer as output file.
prn = open("PRN:", "wt")

# Send contents of text widget to printer file.
prn.write(text_widget.get())

# And send a form feed for good measure.
prn.write(chr(12))
prn.close()
...<end python code>

> -I use a "text" control (I dislike the "widget" term, sorry), 
>  but I can't 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.
> 

Yes indeed.

When you .pack() your text widget, be sure to add the following
arguments to pack():

    fill="x|y|both"   -> tells your text widget that you want it
                         to fill the current space in x, y or both
                         directions.

    expand="yes"      -> tells the text widget to grow along with
                         it's container.

> -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=OpenRecent(i))
> 

OK, you've a problem here.  "Openrecent(i)" is a function call,
so unless it returns a function reference (er, "function pointer")
as a result, then it's surely not doing what you want.

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

er... this ain't Lispy nor Perly ;-)

> 
> (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?
> 

Most probably, a function reference, dynamically created,
which upon evaluation calls your given function with the
arguments you defined for it.  You Want A Lambda (run! hide!).

Does this work?

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

> 
> Has someone any information on one of these problems? It 
> would be great.
>
> Thanks in advance.
>

You're welcome, happy pythoning :-)

-gca





More information about the Python-list mailing list