423,846 Members | 2,048 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,846 IT Pros & Developers. It's quick & easy.

Can the window size of tfFileDialog be changed?

P: 2
The tkFileDialog module is a handy tool for opening and saving files in Python GUI's with TkInter. However, the pop-up window generated by, e.g., tkFileDialog.askopenfilename is rather small (though it can be resized manually). Is there a possibility to resize it automatically?
Feb 18 '11 #1
Share this Question
Share on Google+
5 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
The documentation I have provides no way to size the tkFileDialog.askopenfilename dialog box.
Feb 18 '11 #2

P: 2
Thanks. I haven't found anything either, but I hoped someone has figured out a trick.
Feb 18 '11 #3

P: 2
Yes there is a trick. Complicated, because in fact this very function of tkinter uses the system "Open" window capability, instead of creating a whole dialog window with tkinter widgets.

So you have to :
- find which instruction of your operating system resizes windows (mine ins Linux, so it is "wmctrl -r <window title> -e 0,0,0,<width>,<height>"
- use the os.system() function to pass this instruction from python to the OS
- use the multi-threading capabilities of Python, so that once the window exist, which means that the program is hanging because it waits for your answer, you can resize it through another thread that runs while the main thread is hanging.

I shall return to this site with an example later on.
Jun 14 '15 #4

P: 2
Here is a code example:


Expand|Select|Wrap|Line Numbers
  1. # http://bytes.com/topic/python/answers/908537-can-window-size-tffiledialog-changed
  2. # Code written by madpentiste
  3. #
  4.  
  5. '''
  6. The code herein creates a tkinter dialog "askopenfilename" with window resizing (not supported by default) via linux called from within python
  7.  
  8. It is an adaptation of two recipes (need to scroll down to find the proper code):
  9.  
  10.     http://stackoverflow.com/questions/19790570/python-global-variable-with-thread
  11.  
  12.     http://stackoverflow.com/questions/15528939/python-3-timed-input
  13.  
  14. '''
  15.  
  16.  
  17. from tkinter import *
  18. from threading import Thread
  19. import os, time
  20.  
  21. # Below : instructions that will resize the window, which must be inside a function in order to be called by the threading.Thread() function
  22.  
  23. def resize (windowTitle):
  24.     print('starting resize')
  25.     waitingtime = 0.05
  26.     time.sleep(waitingtime) # waitingtime must be such that the window to be resized exists before the instruction below is executed
  27.     os.system("wmctrl -r " + windowTitle + " -e 0,0,0,1600,900")  # system instruction to resize window named "Title" at width 1600 and height 900
  28.     print('resize done')
  29.  
  30. # Below : instructions that will create the window (to be resized), also inside a function to be called by threading.Thread()
  31.  
  32. def ask_openfilename(windowTitle):
  33.     global filename
  34.     Racine=Tk()
  35.     w = Frame(Racine)
  36.     Racine.withdraw()
  37.     file=w.tk.call('tk_getOpenFile', '-title', windowTitle) # Can add more arguments here see list at https://www.tcl.tk/man/tcl8.4/TkCmd/getOpenFile.htm
  38.                                                             # but beware of the python syntax for w.tk.call, which is << , '-dash_and_argument_name_in_quotes' , argument_value >>.
  39.                                                             # Must add arguments values to the function definition and the tuple of args in << thread_filename = ... >>
  40.  
  41.     filename=str(file)
  42.     return filename
  43.     w.destroy()
  44.     mainloop()
  45.  
  46.  
  47. windowTitle = 'Whichever title for the askopenfilename window'
  48. thread_filename = Thread(target = ask_openfilename, args=(windowTitle,))
  49. thread_resize = Thread(target = resize, args=(windowTitle,))
  50. thread_filename.start()
  51. thread_resize.start()
  52. thread_filename.join()
  53. thread_resize.join()
  54.  
  55. print('Chosen file', filename)
  56.  
  57.  
  58.  
Jun 14 '15 #5

P: 1
As a follow on to madpentiste's code, I attempted to add the following two lines after line 37 (leaving the rest of the code unchanged):

Expand|Select|Wrap|Line Numbers
  1. w. tk .call('set', '::tk::dialog::file::showHiddenBtn', '1')
  2. w. tk .call('set', '::tk::dialog::file::showHiddenVar', '0')
  3.  
and got the error:

parent namespace doesn't exist

This code has worked in this context before. I'm sure this is a stupid error on my part, but I'm not a Tcl.Tk savant.
1 Week Ago #6

Post your reply

Sign in to post your reply or Sign up for a free account.