Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change window size of tkFileDialog.askdirectory?

Tags:

python

tkinter

I've written a short script for finding and saving certain files. I used the following line to pick a save location:

ask_dir = tkFileDialog.askdirectory(initialdir= os.path.dirname(sys.argv[0]))

However, the askdirectory window (at least on my WinXP machine) is excessively small, and it is not resizable. Tkinter does not seem to have any obvious command for increasing this window size - see link here. How can I fix this?

like image 518
bigcat42 Avatar asked Feb 04 '14 16:02

bigcat42


1 Answers

On Windows, the FolderBrowserDialog function seems to be used, which can't be resized easily, although it probably can be resized with some effort.

Some clues can be found here, this code can be 'translated' to Python using the pywin32 module.

Here's some basic code to resize the window, to illustrate how this would work:

import win32gui

win = win32gui.FindWindowEx(None, None, "NULL,"#32770", None)
win32gui.SetWindowPos(win, 0, 500, 500, 900, 900, 0)

You will also need to a) fetch current position, and use that instead of a hardcoded value, and b) resize all the widgets inside the window (see linked article).

Problems:

  • It's ugly
  • My window class is always set to #32770 (win7, 64-bit), it doesn't look very portable to me ... I didn't investigate as to the cause of this.
  • askdirectory is blocking, so you need to do start a separate thread, poll if the window is open, then resize it. Not only is this ugly, it's also possible the user sees 'flicker' of the old window size, before it's resized.

I understand your problem, and the 'Open folder' dialog has a horrible usability, but it would seem significant amount of effort is required to increase the size.
Replacing it with something else might be an option, although that would break the OS's UI conventions, which also isn't good ...

like image 80
Martin Tournoij Avatar answered Sep 28 '22 06:09

Martin Tournoij