[Tutor] tkFileDialog.Directory
Hello! I want to learn Tkinter and try to build a small File search dialog. Tkinter is nice, but here is a problem where I am stuck: I want to allow the dialog's user to pick a directory. The widget for this is tkFileDialog.Directory. But when I start the Directory-Window, it is possible move the focus back to my File Search dialog. Thus it is possible but not wanted to create several "Pick directory" windows. I tried to make the Directory instance modal by calling .grab_set() but this raises an attribute error. Here is a minimal example: --- snip --- import Tix from Tkconstants import * from tkFileDialog import Directory class FileSearchBase: def __init__(self, root): self.root = root self.top = None self.title= "File Search" def open(self): if not self.top: self.create_widgets() else: self.top.deiconify() self.top.tkraise() self.top.grab_set() def close(self, event=None): if self.top is not None: self.top.grab_release() self.top.withdraw() def create_widgets(self): top = Tix.Toplevel(self.root) top.resizable(0,0) top.bind("", self.close) top.protocol("WM_DELETE_WINDOW", self.close) top.wm_title(self.title) self.top = top pickButton= Tix.Button(top, text = 'Pick..', command = self.pick_dir) pickButton.grid(row= 0, column=1) def pick_dir(self): dir_dlg= Directory() #dir_dlg.grab_set() # AttributeError: grab_set #self.top.wait_window(dir_dlg) # AttributeError: _w dir= dir_dlg.show() print dir if __name__ == '__main__': root = Tix.Tk() root.title("File Search Demo") widget = FileSearchBase(root) searchButton = Tix.Button(root, text = 'Show search dialog', command = widget.open) searchButton.pack(padx = 8, pady = 8) root.mainloop() --- snip --- The above example allows to create several Directory dialogs. On the other hand, if I start the Directory dialog from the root window (from the Tix.Tk() instance), everything works fine. The following example works: --- snip --- import Tix from Tkconstants import * from tkFileDialog import Directory def pick_dir(): dir_dlg= Directory() dir= dir_dlg.show() print dir if __name__ == '__main__': root = Tix.Tk() root.title("File Search Demo") pickButton = Tix.Button(root, text = 'Choose Directory', command = pick_dir) pickButton.pack(side = 'bottom') root.mainloop() --- snip --- Would be nice if someone could help me with the first example... Kind regards, Karsten. -- Lust, ein paar Euro nebenbei zu verdienen? Ohne Kosten, ohne Risiko! Satte Provisionen für GMX Partner: http://www.gmx.net/de/go/partner ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] tkFileDialog.Directory
Hello! Michael Lange <[EMAIL PROTECTED]> wrote: > I guess the tkFileDialog.Directory class isn't intended to be used directly. > Try tkFileDialog.askdirectory() instead. > If there are problems with the grab state, try passing "parent=self.top" to askdirectory(). That did it! Thanks a lot! I would like to ask another question. I don't understand the exception mechanism of Python when running a Tkinter app. If an exception happens (it does happen quite often at the moment..), a traceback is written to the console while the Tk window remains open and the application is still running. This is not what I expected -- I expected the application would end. Why is that? Is there a way to create an error handler for uncaught exceptions in Tkinter apps? In other words, can I change the behaviour from writing a traceback to the console to something else? Can I, for example, show a message box instead? Here is a small example: --- snip --- import Tix def raise_exception(): print 1/0 if __name__ == '__main__': root = Tix.Tk() root.title("Exception demo") Tix.Button(root, text = "Don't press", command = raise_exception).pack() try: root.mainloop() except: print "An error has occured." --- snip --- The except part gets never executed. Kind regards, Karsten. -- Telefonieren Sie schon oder sparen Sie noch? NEU: GMX Phone_Flat http://www.gmx.net/de/go/telefonie ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Tkinter mainloop (was Re: tkFileDialog.Directory)
Hello Michael, hello list, thanks for the info that pmw displays exceptions. What I don't understand is >> --- snip --- >> import Tix >> >> def raise_exception(): >> print 1/0 >> >> if __name__ == '__main__': >> root = Tix.Tk() >> root.title("Exception demo") >> >> Tix.Button(root, text = "Don't press", command = raise_exception).pack() >> >> try: >> root.mainloop() >> except: >> print "An error has occured." >> --- snip --- >> >> The except part gets never executed. > That's because the error isn't in the mainloop() method I thought the mainloop() function is something like def mainloop(): e= get_event() if e: for w in widgets: w.handle(e) but apparently it is not. It's not bad that the Tkinter windows don't destroy upon an exception, since it gives me the option to display an error window, but I feel unsafe unless I understand why it does not. -- Telefonieren Sie schon oder sparen Sie noch? NEU: GMX Phone_Flat http://www.gmx.net/de/go/telefonie ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter mainloop
Hello, Thanks a lot for the pointers, Kent, and the explanations, Michael! > There is an undocumented hook that lets you change this - the function > Tk.report_callback_exception() is called to actually report the error. > You can redefine this function to do what you want. Here are a couple > of examples: > http://zephyrfalcon.org/weblog/arch_d7_2003_01_04.html > http://groups.google.com/group/comp.lang.python/browse_thread/thread/ce0036f41da8a22f/c62177e5bb59b09c%23c62177e5bb59b09c?sa=X&oi=groupsr&start=1&num=3 I think I see much clearer now. My little filesearch dialog makes progress. Have a nice weekend! Karsten. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Tkinter modal dialogs
Hello tutors! My file search dialog is working now! I attach a copy of the module. It would be nice, if someone could look over it and make suggestions to improve it. There are at least two points I find not very elegant in the program: 1. The dialog is ended by raising a SystemExit exception. (I found the idea searching www.koders.com for Tkinter programs.) Here is a small example of the technique: --- snip --- import Tix from Tkconstants import * class YesNo: def __init__(self, root, title="", question=""): self.top= Tix.Toplevel(root) self.top.title(title) self.top.withdraw() # don't show Tix.Label(self.top, text=question).pack() Tix.Button(self.top, text="Yes", command=self.yes_cmd).pack(side=LEFT) Tix.Button(self.top, text="No", command=self.no_cmd).pack(side=RIGHT) def ask(self): self.top.deiconify() self.top.tkraise() self.top.grab_set() try: self.top.mainloop() except SystemExit, answer: self.top.grab_release() self.top.withdraw() return answer def yes_cmd(self, event=None): raise SystemExit, True def no_cmd(self, event=None): raise SystemExit, False if __name__=="__main__": def set_label(): global yes_no_dlg, yes_no_label answer= yes_no_dlg.ask() yes_no_label["text"]= str(answer) yes_no_label.update_idletasks() root = Tix.Tk() root.title("Modal Dialog Demo") yes_no_dlg = YesNo(root, "Question", "Do you find this approach elegant?") yes_no_label= Tix.Label(root, text="Unknown.") yes_no_label.pack() Tix.Button(root, text = 'Question..', command = set_label).pack() Tix.Button(root, text = 'Exit', command = root.destroy).pack() root.mainloop() --- snip --- This is deceiving. A SystemExit exception should, in my opinion, exit the program. Is there an other way to do this? 2. The Listbox in the file search dialog seems to have two selection modes. When scrolling with the page-up/page-down keys, only the underlined selection moves, while the other selection (indicated by a different background color) remains where it is unless I press the space key. That is imo too complicated. Is there a Listbox with only one selection type (or an option to Tix.ScrolledListbox)? Kind regards, Karsten. -- 10 GB Mailbox, 100 FreeSMS/Monat http://www.gmx.net/de/go/topmail +++ GMX - die erste Adresse für Mail, Message, More +++import Tix from Tkconstants import * from tkFileDialog import askdirectory from tkMessageBox import showerror from grep_mod import grep import os, re, thread, time class FileSearchBase: def __init__(self, root, search_func, func_caller): """root... parent window search_func... a "worker" function which performs the search func_caller... the call to the search_func is wrapped to allow threading etc.""" self.root = root self.top = None self.title= "File Search" self.subdirs= Tix.BooleanVar() self.is_regex= Tix.BooleanVar() self.is_stopped= False self.search_func= search_func self.func_caller= func_caller self.time= 0 self.dir= None self.file_combo= None self.search_combo= None self.startstop= None self.statusbar= None def ask_file_by_search(self): self._open() try: self.top.mainloop() except SystemExit, file_name: self._close() return file_name def _open(self): """Window management: displays the File search dialog window""" if not self.top: self._create_widgets() else: self.top.deiconify() self.top.tkraise() self.search_combo.entry.focus_set() self.search_combo.entry.selection_range(0, "end") self.search_combo.entry.icursor(0) self.top.grab_set() def _close(self, event=None): """Window management: closes the File search dialog""" if self.top is not None: self.top.grab_release() self.top.withdraw() def _create_widgets(self): """Window management: called by _open() to create the dialog widgets.""" top = Tix.Toplevel(self.root) top.resizable(0,0) top.bind("", self._start_search_cmd) top.bind("", self._cancel_cmd) top.protocol("WM_DELETE_WINDOW", self._cancel_cmd) top.wm_title(self.title) #top.wm_iconname(self.icon) self.top = top self.top.grid_columnconfigure(0, pad=2, weight=0) self.top.grid_columnconfigure(1, pad=2, minsize=100, weight=100) # first row: folder information ## first column: Folder entry + pick folder button frame= Tix.Frame(top) self.dir = Tix.LabelEntry(frame, label="Folder:", options='entry.width 20') self.dir.entry.insert("end", os.getcwd()) # use os.getcwdu() for unicode self.dir.pack(side=LEFT) w= Tix.Button(frame, text = 'Pick..', command = self._pick_dir_cmd) w.pack(side=RIGHT) frame.grid(row=0, column=0) ## second column: checkbox w= Tix.Checkbutton(top, text = 'Look in subfolders, too', # Label variable=self.subdirs # Variab
Re: [Tutor] Read Excel file without COM
Hello, you could try to access the excel file via ODBC if the only thing you need to do is reading. It worked for me when I was working with Excel 95 (no COM) and wanted to read in worksheets in python. Here are some infos on excel datasources: http://www.idude.net/excel/articles/using_excel_file_datasources.asp I could not access password-protected excel files, the fetchall() method did not work, create/drop index, alter table did not work, table names are like "[sheet1$]" (note the brackets and the dollar sign). Just my 2Cents, Karsten. -- Highspeed-Freiheit. Bei GMX supergünstig, z.B. GMX DSL_Cityflat, DSL-Flatrate für nur 4,99 Euro/Monat* http://www.gmx.net/de/go/dsl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] wxPython
> How can I avoid that the graphics freeze. > here is another example for doing this: http://uucode.com/texts/pylongopgui/pyguiapp.html It uses Tkinter, but it should work in wxPython, too. Kind regards, Karsten. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [Linux] open a file in any home "~" ?
Hi, try f=file(os.path.join(os.path.expanduser("~")),"myfile"), "r") Kind regards, Karsten. -- Echte DSL-Flatrate dauerhaft für 0,- Euro*! "Feel free" mit GMX DSL! http://www.gmx.net/de/go/dsl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] File like object for Windows registry
Hello! My app should run on debian and windows platforms. For storing the configuration data, I use the ConfigParser module. What I find difficult is to determine a place for my configuration file. On debian, it is simply os.path.join(os.path.expanduser("~")),"myconfig") but what am I supposed to do on Windows? I think a clean solution would be to create a file-like object that reads and writes to the registry, is it? Kind regards, Karsten. -- "Feel free" – 10 GB Mailbox, 100 FreeSMS/Monat ... Jetzt GMX TopMail testen: http://www.gmx.net/de/go/topmail ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Config file directory (was: File like object for Windows registry)
Hello again! Thanks a lot for the helpful hints! I was not aware that expanduser does also work on windows since I am on WinMe... My current approach is this: import platform, os.path def _detect_filepath(): op_sys= platform.system() op_rel= platform.release() if op_sys=="Windows": if op_rel in ["XP"]: return os.path.expanduser("~") else: return os.path.dirname(__file__) # store in package directory elif op_sys=="Linux": return os.path.expanduser("~") else: raise "Unsupported operating system." What do you think? If someone with a win2000 system could tell me the return value of platform.release(), that would be nice. Kind regards, Karsten. - Original Message - Hello! My app should run on debian and windows platforms. For storing the configuration data, I use the ConfigParser module. What I find difficult is to determine a place for my configuration file. On debian, it is simply os.path.join(os.path.expanduser("~")),"myconfig") but what am I supposed to do on Windows? I think a clean solution would be to create a file-like object that reads and writes to the registry, is it? Kind regards, Karsten. -- Echte DSL-Flatrate dauerhaft für 0,- Euro*. Nur noch kurze Zeit! "Feel free" mit GMX DSL: http://www.gmx.net/de/go/dsl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor