Can the window size of tfFileDialog be changed?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JurVogelzang
    New Member
    • Feb 2011
    • 2

    Can the window size of tfFileDialog be changed?

    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.as kopenfilename is rather small (though it can be resized manually). Is there a possibility to resize it automatically?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    The documentation I have provides no way to size the tkFileDialog.as kopenfilename dialog box.

    Comment

    • JurVogelzang
      New Member
      • Feb 2011
      • 2

      #3
      Thanks. I haven't found anything either, but I hoped someone has figured out a trick.

      Comment

      • madpentiste
        New Member
        • Jun 2015
        • 2

        #4
        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.

        Comment

        • madpentiste
          New Member
          • Jun 2015
          • 2

          #5
          Here is a code example:


          Code:
          # http://bytes.com/topic/python/answers/908537-can-window-size-tffiledialog-changed
          # 
          # Code written by madpentiste
          #
          
          '''
          The code herein creates a tkinter dialog "askopenfilename" with window resizing (not supported by default) via linux called from within python
          
          It is an adaptation of two recipes (need to scroll down to find the proper code):
          
              http://stackoverflow.com/questions/19790570/python-global-variable-with-thread
          
              http://stackoverflow.com/questions/15528939/python-3-timed-input
          
          '''
          
          
          from tkinter import *
          from threading import Thread
          import os, time
          
          # Below : instructions that will resize the window, which must be inside a function in order to be called by the threading.Thread() function
          
          def resize (windowTitle):
              print('starting resize')
              waitingtime = 0.05
              time.sleep(waitingtime) # waitingtime must be such that the window to be resized exists before the instruction below is executed
              os.system("wmctrl -r " + windowTitle + " -e 0,0,0,1600,900")  # system instruction to resize window named "Title" at width 1600 and height 900
              print('resize done')
              
          # Below : instructions that will create the window (to be resized), also inside a function to be called by threading.Thread()
          
          def ask_openfilename(windowTitle):
              global filename
              Racine=Tk()
              w = Frame(Racine)
              Racine.withdraw()
              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
                                                                      # but beware of the python syntax for w.tk.call, which is << , '-dash_and_argument_name_in_quotes' , argument_value >>.
                                                                      # Must add arguments values to the function definition and the tuple of args in << thread_filename = ... >>
          
              filename=str(file)
              return filename
              w.destroy()
              mainloop()
          
          
          windowTitle = 'Whichever title for the askopenfilename window'
          thread_filename = Thread(target = ask_openfilename, args=(windowTitle,))
          thread_resize = Thread(target = resize, args=(windowTitle,))
          thread_filename.start()
          thread_resize.start()
          thread_filename.join()
          thread_resize.join()
          
          print('Chosen file', filename)

          Comment

          • BC79
            New Member
            • Dec 2018
            • 1

            #6
            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):

            Code:
            w. tk .call('set', '::tk::dialog::file::showHiddenBtn', '1')
            w. tk .call('set', '::tk::dialog::file::showHiddenVar', '0')
            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.
            Last edited by BC79; Dec 3 '18, 09:45 PM. Reason: to clarify code fragment

            Comment

            Working...