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?
5 8441 bvdet 2,851
Expert Mod 2GB
The documentation I have provides no way to size the tkFileDialog.askopenfilename dialog box.
Thanks. I haven't found anything either, but I hoped someone has figured out a trick.
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.
Here is a code example: -
# 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)
-
-
-
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): - 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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Put 030516 in email subj to get thru |
last post by:
I've always been bothered about having to statically declare the size of
a Java applet window (container?) in the calling HTML. I've always
wanted the moral equivalent of width=50% statement (of...
|
by: spencer |
last post by:
Hello,
I have an index page with an autoscroller writen with CSS.
The problem is the scrolling content(text and inages)'s position is correct
but will off position when the broswer(IE) window...
|
by: deko |
last post by:
When a user opens my Access database application, I would like the
application window to be a certain size (but not maximized). Currently,
when my app is first opened, the application window...
|
by: Lauren Quantrell |
last post by:
Is there a way in code to freeze the size of the MS Access window or to
prevent it from being sized?
Thanks.
lq
|
by: Kenny |
last post by:
The following link will take you to menu of photo galleries. If you open any
of the photo galleries you will see that each contains thumbnails. When a
thumbnail is clicked, the full-sized version...
|
by: John Fox |
last post by:
Dear All,
How do I set the size of the window that is showing the database forms?
can't find any helps on it.
John Fox
|
by: bbqbbq |
last post by:
Hi guys,
I need a way to automatically adjust the layout of window's content when the window's size get changed. For example, I am creating a web page using ASP.NET. However, when I drag the...
|
by: jobs |
last post by:
I have a simple login page with roughly a 300px x 300px image behind
the asp.net login control. I need the login control to land in the
middle of the image. I want both the image and the login...
|
by: =?Utf-8?B?V29ua28gdGhlIFNhbmU=?= |
last post by:
Hello--
Is there a "WPF way" of telling why a Window's size was changed? In other
words, is there a way to tell if the dialog was dragged bigger by the user
versus a window automatically...
|
by: Annette Block |
last post by:
I'm rather new in JavaScript, but I have some experience in php.
I learned it's rather easy to open a window of a specified size with
JavaScript, that you need to specify the opened file, but I...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |