Hello File.py

L

Lith

Here's a simple script, where I want to use a GUI to select a file.
(And eventually do something interesting with it.) Some problems
appear.

(1) Why does it call MenuOpen automatically?

(2) Why won't it open when I choose the menu item?

(3) Python Cookbook gave me the sys.exit call, if I run it from IDLE it
gives me a traceback. Should I be using something else?

(4) Does tkFileDialog have any useful official documentation? The main
google results for it don't even give the possible parameters for the
function calls, or sample code showing how to use.

(5) Is the 'parent = parent,' line just plain wrong?

Feel free to note any horrors you see.


...........................

import sys
from Tkinter import *
import tkFileDialog

def MenuOpen(parent):
tkFileDialog.askopenfilename(
defaultextension = '.ctb',
filetypes = [
('AutoCAD Color-Dependent Style Table Files', '*.ctb'),
('AutoCAD Named Style Table Files', '*.stb'),
('All Files', '*.*')
],
initialdir = '\\\\Desktop',
initialfile = '',
parent = parent,
title = 'Open Plot Style File...'
)


def DoInterface():
root = Tk()

menubar = Menu(root)
root.config(menu = menubar)

menuFile = Menu(menubar)
menuFile.add_command( label='Open',
command=MenuOpen(parent=root))
menuFile.add_separator()
menuFile.add_command( label='Exit', command=sys.exit)

menubar.add_cascade( label='File', menu=menuFile);

root.mainloop()

#----------------------------------------------------------------

DoInterface()
 
E

Eric Brunel

Here's a simple script, where I want to use a GUI to select a file.
(And eventually do something interesting with it.) Some problems
appear.

(1) Why does it call MenuOpen automatically?

Because this code:
menuFile.add_command(label='Open', command=MenuOpen(parent=root))

*calls* your MenuOpen function and assign its *returned value* to the command option in your menu. You need to pass the function itself, not call it. In fact, you did it correctly in:
menuFile.add_command( label='Exit', command=sys.exit)

Here, you pass the sys.exit function. What you did above is as if you wrote:
menuFile.add_command(label='Exit', command=sys.exit())
With this line, sys.exit gets called and your application will terminate immediatly.

As for the line above, the general solution is to do:

menuFile.add_command(label='Open', command=lambda root=root: MenuOpen(root))

The 'lambda' stuff creates an anonymous function that will call your MenuOpen function when the menu is selected. For more details, please refer to the Python tutorial - http://docs.python.org/tut/node6.html#SECTION006750000000000000000

But here, you don't even need that, since the root window is the default parent for all Tkinter dialogs, so doing:

menuFile.add_command(label='Open', command=MenuOpen)

is enough.
(2) Why won't it open when I choose the menu item?

Since you assigned the value returned by MenuOpen to the command option, and since MenuOpen doesn't return anything, there is no command associated to your menu and it does nothing at all.
(3) Python Cookbook gave me the sys.exit call, if I run it from IDLE it
gives me a traceback. Should I be using something else?

No you shouldn't; I don't know exactly how IDLE handles this, but the way sys.exit() terminates your program is actually by raising an exception. In regular usage, i.e. if you run your Python script from the command line, you'll never see this exception. Maybe IDLE treats it like any other one; any experience on this topic, anyone?
(4) Does tkFileDialog have any useful official documentation? The main
google results for it don't even give the possible parameters for the
function calls, or sample code showing how to use.

The best documentation you'll ever find is the tcl/tk one, e.g. here: http://www.tcl.tk/man/tcl8.4/TkCmd/contents.htm
The corresponding functions are tk_getOpenFile & tk_getSaveFile; you'll just have to convert the '-option value' syntax to 'option=value' to be able to use it via Tkinter.
(5) Is the 'parent = parent,' line just plain wrong?

It may look so, but it isn't: the parent at the left is the name for a keyword parameter for the function and the one at the right is the name of a variable. There is no possible confusion here, and this form is actually quite frequent.
Feel free to note any horrors you see.

Just one little thing: if you declare:

def MenuOpen(parent): ...

you'd better call it via:

MenuOpen(root)

and not:

MenuOpen(parent=root)

The 'parent=' stuff is unnecessary and may be confusing, since this syntax is often used only when you declare a function with keyword parameters like:

def f(arg, **kwdParams): ...

HTH
- Eric Brunel -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top