Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2.7/Windows: How to control position of Tkinter common dialogs?

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

like image 998
Malcolm Avatar asked Oct 22 '10 17:10

Malcolm


1 Answers

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.

like image 84
patthoyts Avatar answered Sep 28 '22 06:09

patthoyts