Python 2.7 under Windows: How can we control the position of Tkinter's common dialogs?
Here's what we've discovered:
Certain common dialogs always open up relative to their parent window
Certain common dialogs always open up centered on the user's desktop
All common dialogs appear to ignore the optional parent= parameter
Questions:
How can we force a dialog to open up relative to its parent window?
How can we force a dialog to open up centered on the user's desktop?
Background:
import tkColorChooser as colorchooser
import tkFileDialog as filedialog
import tkMessageBox as messagebox
; # always open up relative to parent windows
fileOpen = filedialog.askopenfilename()
fileOpens = filedialog.askopenfilenames()
fileSaveAs = filedialog.asksaveasfilename()
color = colorchooser.askcolor()
; # always open up centered on desktop
folderOpen = filedialog.askdirectory()
messagebox.askquestion()
Thank you,
Malcolm
For the Windows messagebox you can't. It appears in the center of the screen and that is that. However, the file selection dialog and color chooser are system dialogs that have been given a Tk wrapper so that users see the stock dialogs on this platform. If you set the -parent option then this is passed through to the wrapped windows and it will center itself over your designated toplevel.
In Tk:toplevel .t
tk_chooseColor -parent .t
How you turn that into Tkinter I leave to someone with some Python experience.
As for centering these, the hwndOwner member of the CHOOSECOLOR structure is always set to the HWND for one of your Tk toplevels. To let it parent against the desktop you'd need to pass NULL there and Tk doesn't let you. You could source the unix version (lib/clrpick.tcl) and show that instead but it would then look weird on a Windows desktop.
Related
I was wondering if there is anyway to get the ico file of one window and use it in the same window, without getting to know the icon location.
from tkinter import *
root = Tk()
root.iconbitmap('img/icn.ico')
top = Toplevel()
root.mainloop()
Here I want top to have icon of root without saying top.iconbitmap() or top.iconphoto(), the closest ive got is top.tk.call('wm','iconbitmap') but I dont know what is to be done with this as i couldnt find a understandable documentation.
Why dont I want to use iconbitmap(), its basically that, with tkinter.messagebox you can see the messagebox automatically inherit the icons from the parent widget. I was trying to duplicate this effect. Where if the icon is the default tk icon, then show blank icon or else show the custom icon.
Thanks in advance :D
[I'm using links into the core Tk documentation here. It's much more accurate than the Tkinter docs for most things, and Tkinter is mostly an obvious thin wrapper around it.]
You don't want wm iconbitmap. That's been effectively obsolete for decades; it uses an object class — bitmap — that's not relevant these days as it is monochrome and uses the weirdest format. (Filenames need to be preceded by # to make them work.)
Instead, you want to manipulate the wm iconphoto of the toplevel windows concerned. These take true photo images (there are many image file formats you can load into them) and you can share them easily.
# Load the image from the file; can also use PNG and other formats
my_image = PhotoImage(file="image.gif")
# Apply the image as the icons
first_toplevel_window.iconphoto(False, my_image)
second_toplevel_window.iconphoto(False, my_image)
Note that how the icon is displayed can vary wildly; it's not under your control.
You can use iconphoto() and set the first argument to True, then the same icon will be used for future created toplevels as well:
import tkinter as tk
root = tk.Tk()
icn = tk.PhotoImage(file='my-icon.png')
root.iconphoto(True, icn)
top = tk.Toplevel(root)
root.mainloop()
If you use the default instead of the bitmap (or first) argument, the icon will automatically be used on all TopLevel windows:
root.iconbitmap('img/icn.ico') # icon set only on root
root.iconbitmap(bitmap='img/icn.ico') # same as above
root.iconbitmap(default='img/icn.ico') # icon set on root and all TopLevels
I am writing a python app using Tkinter and I cannot find a way to change the default title Tk in OS top bar (Gnome top bar and dock too).
I can set the main window title but it does not change the string in the top bar where it stays Tk. Honestly I am surprised I cannot find anyone asking this same question.
I am developing on Ubuntu 18.04 using Python 3.6
This is a sample code to replicate the problem:
import tkinter as tk
if __name__ == "__main__":
mainWindow = tk.Tk()
mainWindow.title("TEST")
mainWindow.geometry("1024x600")
mainWindow.mainloop()
I would expect to read the string TEST in the main window frame title, in the OS top bar and in the OS application dock, but I get it only in the windows frame title. The rest display Tk
Use className
mainWindow = tk.Tk(className='Ghanshyam')
According to python Docs on tkinter:
The Tk class is instantiated without arguments. This creates a toplevel widget of Tk which usually is the main window of an application. Each instance has its own associated Tcl interpreter.
The top bar title in Ubuntu is not set by the application running, it is set by the desktop entry for the app. I'm not sure why it works the way it does when there is no desktop entry present, but you can create a file like this and import it into Ubuntu for it to work properly.
[Desktop Entry]
Type=Application
Terminal=false
Icon=/path/to/icon/icon.png
Name=TITLE (this is what handles the top bar title)
Exec=/path/to/file/executable
Categories=Utility;
See this question on AskUbuntu.
I know this might be obvious, but in tkinter you can set an icon, but I have found it really hard to find one. I just wanted to know if you have to use the .ico format for the file or if there is a way to use .png or .jpeg files.
Currently I have
window = Tkinter.Tk()
window.title("Weclome!")
window.geometry("200x300")
window.wm_iconbitmap("Icon.ico")
window.configure(background = "Black")
That is the whole setup I have and I just want to know about line 4:
window.wm_iconbitmap("Icon.ico")
Thanks for responding to my question, although i am sorry for not spending more time looking into the question rather than just asking here.
Let's start by reading the documentation!
The documentation at effbot.org says the following regarding iconbitmap(bitmap=None)
Sets or gets the icon bitmap to use when this window is iconified. This method is ignored by some window managers (including Windows).
Note that this method can only be used to display monochrome icons. To display a color icon, put it in a Label widget and display it using the iconwindow method instead.
Same as wm_iconbitmap.
So here's the documentation about iconwindow(window=None):
Sets or gets the icon window to use as an icon when this window is iconified. This method is ignored by some window managers (including Windows).
Same as wm_iconwindow.
window
The new icon window. If omitted, the current window is returned.
According to this other documentation, which actually says the same things as the docstrings of the homonymous method for tkinter in (at least) Python 2.7, 3.5 and 3.6:
wm_iconbitmap(self, bitmap=None, default=None)
Set bitmap for the iconified widget to bitmap. Return the bitmap if None is given.
Under Windows, the default parameter can be used to set the icon for the widget and any descendents that don't have an icon set explicitly. default can be the relative path to a .ico file (example: root.iconbitmap(default='myicon.ico') ). See Tk documentation for more information.
So here's the original Tk documentation:
wm iconbitmap window ?bitmap?
If bitmap is specified, then it names a bitmap in the standard forms accepted by Tk (see the Tk_GetBitmap manual entry for details). This bitmap is passed to the window manager to be displayed in window's icon, and the command returns an empty string. If an empty string is specified for bitmap, then any current icon bitmap is canceled for window. If bitmap is specified then the command returns an empty string. Otherwise, it returns the name of the current icon bitmap associated with window, or an empty string if window has no icon bitmap.
From my understanding of Tcl, here window is your toplevel window (either an instance of Tk or Toplevel).
On the Windows operating system, an additional flag is supported:
wm iconbitmap window ?-default? ?image?
If the -default flag is given, the icon is applied to all toplevel windows (existing and future) to which no other specific icon has yet been applied.
In addition to bitmap image types, a full path specification to any file which contains a valid Windows icon is also accepted (usually .ico or .icr files), or any file for which the shell has assigned an icon.
Tcl will first test if the file contains an icon, then if it has an assigned icon, and finally, if that fails, test for a bitmap.
Not very concrete and thus helpful answer so far.
My conclusion
The iconbitmap function (or method, depending on the programming language) should be used to set a bitmap image to the window when the window is iconified.
On Windows you're allowed to set a full path specification to any file which contains a valid Windows icon is also accepted (usually .ico or .icr files), or any file for which the shell has assigned an icon.
So which images are bitmaps?
xbm and xpm (for X Window System)
According to the Wikipedia article to which I linked "bitmap" to above:
The X Window System uses a similar xbm format for black-and-white images, and xpm for color images.
...
BMP file format
Netpbm format
.wbmp
ILBM
...
So most of the bitmap file formats are not cross-platform! In other words, if someone tells you to use a xbm image for the icon, it may not work on your platform because xbm are bitmaps for X Window System.
Note: even after this answer you may still have problems!
Other possible useful articles
Set window icon
tkinter TclError: error reading bitmap file
I was struggling a lot to find an answer too but finally I peeked into the source code of idle3.6 where I found the following piece of code:
# set application icon
icondir = os.path.join(os.path.dirname(__file__), 'Icons')
if system() == 'Windows':
iconfile = os.path.join(icondir, 'idle.ico')
root.wm_iconbitmap(default=iconfile)
else:
ext = '.png' if TkVersion >= 8.6 else '.gif'
iconfiles = [os.path.join(icondir, 'idle_%d%s' % (size, ext))
for size in (16, 32, 48)]
icons = [PhotoImage(master=root, file=iconfile)
for iconfile in iconfiles]
root.wm_iconphoto(True, *icons)
I searched through all files in the idlelib folder for .ico and .png using the rummage software.
So finally I managed to get the window icon working (on GNU-linux with TkVersion>=8.6) with the following two lines:
icon = PhotoImage(master=root, file='icon.png')
root.wm_iconphoto(True, icon)
where I put the icon directly in my application folder.
From the idle code it seems to me that on Windows still only .ico files are supported.
I have this simple example of the behavior:
import tkinter as tk
from tkinter import filedialog, ttk
INITIALDIR = 'C:\\'
class MainWindow(ttk.Frame):
def __init__(self, root, *args, **kwargs):
super().__init__(root, *args, **kwargs)
self.pack()
btnoptions = {'expand':True, 'fill': 'both'}
btn = ttk.Button(self, text='Select', command=self.ask_openfile)
btn.pack(**btnoptions)
def ask_openfile(self):
self.file_opt = options = {}
options['initialdir'] = INITIALDIR
filename = filedialog.askopenfilename(**self.file_opt)
return filename
if __name__=='__main__':
root = tk.Tk()
root.geometry('600x300')
MainWindow(root).pack(expand=True, fill='both', side='top')
root.mainloop()
Basically there is one big button, which opens a open file dialog. If I select a file and press open, it works fine. However, if I double click to select a file, it selects the file, closes the dialog, and immediately opens a new open file dialog. My guess, that the second click somehow is passed to the underlying window and it clicks on the button again (button has to be under the file which is about to be selected). Is there a way to avoid this behavior? Looks like it is Windows problem, tried on windows 7 and 10 with python 3.5. On debian linux everything is fine, however, I need this to work on Windows.
Seems like this is a known issue with tk:
https://core.tcl.tk/tk/tktview?name=faf37bd379
That ticket says that the issue is fixed in tk 8.6.8.
I was having this issue with tk 8.6 and updating isn't a great option so I tried to find some other workarounds. I tried disabling the buttons and then enabling them but the rouge click just occurred after the button was enabled. I also tried adding a delay before enabling the buttons again but that didn't work either.
The two workarounds that I found that actually worked are:
1) Change the buttons to require a double-click. This can be done with bind . I don't personally like having to double-click the button but maybe other people are less picky.
2) Change the file selection to a combobox with a list of the files in a selected directory. The directory can be selected using askdirectory in place of askopenfilename. askdirectory requires the user to click the "Select Folder" button so it does not have the same issue with double-clicks.
I'm using OS X. I'm double clicking my script to run it from Finder. This script imports and runs the function below.
I'd like the script to present a Tkinter open file dialog and return a list of files selected.
Here's what I have so far:
def open_files(starting_dir):
"""Returns list of filenames+paths given starting dir"""
import Tkinter
import tkFileDialog
root = Tkinter.Tk()
root.withdraw() # Hide root window
filenames = tkFileDialog.askopenfilenames(parent=root,initialdir=starting_dir)
return list(filenames)
I double click the script, terminal opens, the Tkinter file dialog opens. The problem is that the file dialog is behind the terminal.
Is there a way to suppress the terminal or ensure the file dialog ends up on top?
Thanks,
Wes
For anybody that ends up here via Google (like I did), here is a hack I've devised that works in both Windows and Ubuntu. In my case, I actually still need the terminal, but just want the dialog to be on top when displayed.
# Make a top-level instance and hide since it is ugly and big.
root = Tkinter.Tk()
root.withdraw()
# Make it almost invisible - no decorations, 0 size, top left corner.
root.overrideredirect(True)
root.geometry('0x0+0+0')
# Show window again and lift it to top so it can get focus,
# otherwise dialogs will end up behind the terminal.
root.deiconify()
root.lift()
root.focus_force()
filenames = tkFileDialog.askopenfilenames(parent=root) # Or some other dialog
# Get rid of the top-level instance once to make it actually invisible.
root.destroy()
Use AppleEvents to give focus to Python. Eg:
import os
os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')
I had this issue with the window behind Spyder:
root = tk.Tk()
root.overrideredirect(True)
root.geometry('0x0+0+0')
root.focus_force()
FT = [("%s files" % ftype, "*.%s" % ftype), ('All Files', '*.*')]
ttl = 'Select File'
File = filedialog.askopenfilename(parent=root, title=ttl, filetypes=FT)
root.withdraw()
filenames = tkFileDialog.askopenfilenames(parent=root,initialdir=starting_dir)
Well parent=root is enough for making tkFileDialog on top. It simply means that your root is not on top, try making root on top and automatically tkFileDialog will take top of the parent.
None of the other answers above worked for me 100% of the time.
In the end, what worked for me was adding 2 attibutes: -alpha and -topmost
This will force the window to be always on top, which was what I wanted.
import tkinter as tk
root = tk.Tk()
# Hide the window
root.attributes('-alpha', 0.0)
# Always have it on top
root.attributes('-topmost', True)
file_name = tk.filedialog.askopenfilename( parent=root,
title='Open file',
initialdir=starting_dir,
filetypes=[("text files", "*.txt")])
# Destroy the window when the file dialog is finished
root.destroy()
Try the focus_set method. For more, see the Dialog Windows page in PythonWare's An Introduction to Tkinter.