Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99660

Re: askopenfilename()

From Christian Gollwitzer <auriocus@gmx.de>
Newsgroups comp.lang.python
Subject Re: askopenfilename()
Date 2015-11-28 12:45 +0100
Organization A noiseless patient Spider
Message-ID <n3c3vu$a8t$1@dont-email.me> (permalink)
References (6 earlier) <n2kqvn$8de$1@news2.informatik.uni-stuttgart.de> <564DF94E.8020302@gmail.com> <mailman.509.1448009036.16136.python-list@python.org> <n2vgq3$76g$1@news2.informatik.uni-stuttgart.de> <n3bvn3$k2f$1@news2.informatik.uni-stuttgart.de>

Show all headers | View raw


Am 28.11.15 um 11:29 schrieb Ulli Horlacher:
> One of my Windows test users reports, that the file dialog window of
> askopenfilename() starts behind the console window and has no focus.
> On Linux (XFCE) I do not have this problem.
>
> I start it with:
>
>    Tk().withdraw()
>    file = askopenfilename(title='select a file',initialdir=HOME)
>    set_window_focus() # give focus back to console window
>
> Can one force askopenfilename() to start in foreground with focus?

I can't test it right now, but I think it /should/ go into the 
foreground by itself. For a toplevel window, which you create manually, 
there is a lift() method which asks the OS to move the window to the 
top. But on Windows, this file dialog is a native call and cannot be 
influenced that much.

I see two thingd:

1) Tk().withdraw()

- this seems odd to me, because you don't keep a reference to the Tk 
object around. Better do

	root=Tk()
	roo.withdraw()

2) Maybe it helps if you inject an update() after the withdraw(), maybe not

	root.update()

3) I can confirm, that also on OSX the file dialog does not raise above 
the console window, though it is not as bad because the dialog window is 
muhc bigger then the console window.

I think that you are curing a lot of symptoms with the focus setting to 
the console etc. Many problems would simply go away if you wrote the 
whole thing as a GUI program. If I understand correctly, what you want - 
a program to select files and folders to upload to your server - then 
this would not be that much more work than the CLI with input() which 
you are writing, and definitely less work to get it correct than the 
plastering of the symptoms.

For example, a very simple approach would use a listbox with a + and a - 
button. upon hitting - (or the delete key), you delete the selected 
entries. Upon hitting +, you pop up the file selection dialog. upon 
hitting a Go button, you send the files to the server. A minimalistic 
version of it is included below. That is less then 50 lines of code, 
including comments, without all the focus problems, providing a standard 
desktop GUI metaphor. I haven't seen your command line code, but I doubt 
that it is significantly simpler.

Of course, the code below can still use a great deal of polishing, like 
scrollbars for the listbox, allowing multiple selection both for the 
file dialog and the listbox, nice icons for the buttons, trapping the 
close button on the main window with an "are you sure?"-type question, 
maybe wrapping it up in a class, a progress bar during the upload and a 
way to interrupt it... which is left as an exercise to the reader.

	Christian

=================================
import Tkinter as tk, tkFileDialog as fd, ttk
from Tkinter import N,S,W,E
# create one ttk::frame to fill the main window
root=tk.Tk()
main=ttk.Frame(master=root)
# tell the pack geometry manager to completely
# fill the toplevel with this frame
main.pack(fill=tk.BOTH, expand=1)

# now create a listbox and a button frame
lb=tk.Listbox(master=main)
bf=ttk.Frame(master=main)

# use the grid manager to stack them, whereby
# the listbox should expand
lb.grid(row=0, column=0, sticky=(N,S,E,W))
bf.grid(row=1, column=0, sticky=(N,S,E,W))
main.rowconfigure(0, weight=1)
main.columnconfigure(0, weight=1)

def addfile():
	filename=fd.askopenfilename()
	if filename:
		lb.insert(tk.END, filename)

def remove(*args):
	sel=lb.curselection()
	if sel:
		lb.delete(sel)

def submit():
	print("Submitting files:")
	for filename in lb.get(0,tk.END):
		print("Sending %s"%filename)

# create the three buttons
btnplus=ttk.Button(master=bf, text="+", command=addfile)
btnminus=ttk.Button(master=bf, text="-", command=remove)
btngo=ttk.Button(master=bf, text="Submit", command=submit)

btnplus.pack(side=tk.LEFT)
btnminus.pack(side=tk.LEFT)
btngo.pack(side=tk.LEFT)

# bind also the delete and Backspace keys
lb.bind('<Delete>', remove)
lb.bind('<BackSpace>', remove)

root.mainloop()
===================================================


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

non-blocking getkey? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-18 08:39 +0000
  Re: non-blocking getkey? Christian Gollwitzer <auriocus@gmx.de> - 2015-11-18 10:14 +0100
    Re: non-blocking getkey? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-18 10:39 +0000
      Re: non-blocking getkey? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-18 11:01 +0000
        Re: non-blocking getkey? Christian Gollwitzer <auriocus@gmx.de> - 2015-11-18 12:17 +0100
        Re: non-blocking getkey? Terry Reedy <tjreedy@udel.edu> - 2015-11-18 07:26 -0500
          Re: non-blocking getkey? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-18 13:06 +0000
            Re: non-blocking getkey? Chris Angelico <rosuav@gmail.com> - 2015-11-19 00:24 +1100
              Re: non-blocking getkey? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-18 13:57 +0000
            Re: non-blocking getkey? Steven D'Aprano <steve@pearwood.info> - 2015-11-19 00:38 +1100
              Re: non-blocking getkey? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-18 13:51 +0000
                Re: non-blocking getkey? Chris Angelico <rosuav@gmail.com> - 2015-11-19 01:01 +1100
                Re: non-blocking getkey? Jussi Piitulainen <harvesting@makes.invalid> - 2015-11-18 16:13 +0200
        Re: non-blocking getkey? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-18 16:50 +0000
          Re: non-blocking getkey? Terry Reedy <tjreedy@udel.edu> - 2015-11-18 12:55 -0500
            Re: non-blocking getkey? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-19 15:48 +0000
              Re: non-blocking getkey? Michael Torrie <torriem@gmail.com> - 2015-11-19 09:31 -0700
              Re: non-blocking getkey? eryksun <eryksun@gmail.com> - 2015-11-20 02:43 -0600
                Re: non-blocking getkey? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-23 17:01 +0000
                askopenfilename() (was: Re: non-blocking getkey?) Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-28 10:29 +0000
                Re: askopenfilename() Christian Gollwitzer <auriocus@gmx.de> - 2015-11-28 12:45 +0100
                Re: askopenfilename() Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-28 12:48 +0000
                Re: askopenfilename() Christian Gollwitzer <auriocus@gmx.de> - 2015-11-28 14:34 +0100
                Re: askopenfilename() Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-28 16:45 +0000
                Re: askopenfilename() Laura Creighton <lac@openend.se> - 2015-11-28 17:02 +0100
                Re: askopenfilename() Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-28 16:05 +0000
        Re: non-blocking getkey? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-12-10 08:28 +0000
          Re: non-blocking getkey? Christian Gollwitzer <auriocus@gmx.de> - 2015-12-10 09:54 +0100
            Re: non-blocking getkey? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-12-10 10:26 +0000
          Re: non-blocking getkey? Christian Gollwitzer <auriocus@gmx.de> - 2015-12-10 09:57 +0100
            Re: non-blocking getkey? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-12-10 10:40 +0000
          Re: non-blocking getkey? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-12-10 15:52 +0000
    Re: non-blocking getkey? eryksun <eryksun@gmail.com> - 2015-11-18 12:24 -0600

csiph-web