Tk Source Code

View Ticket
Login
Ticket UUID: 3414693
Title: tk_dialog: (mis)placement
Type: Bug Version: None
Submitter: zbigniew_b Created on: 2011-09-27 20:04:49
Subsystem: 35. [tk_dialog] Assigned To: fvogel
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2026-10-14 16:36:21
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2026-10-14 16:36:21
Description:
tk_dialog is centered in relation to "physical" screen - and, unfortunately, not to "root window", like e.g. tk_messageBox is.
User Comments: jan.nijtmans added on 2026-10-14 16:36:21:

Fixed in [7607b7988e21530d|trunk], core-9-0-branch and core-8-6-branch

Many thanks!


serhiy.storchaka added on 2026-10-01 18:40:44:

All other Tk dialogs (tk_messageBox, tk_chooseColor, tk_chooseDirectory, tk_getOpenFile, the font chooser) are centered over their parent, and tkinter's simpledialog, which reimplements tk_dialog, does the same.

Proposed fix in branch [tk-dialog-over-parent]: fvogel's patch, plus test dialog-2.4. The parent is the parent of the dialog window (usually "."). The Motif-style placement can still be obtained: if the parent is not mapped (e.g. a withdrawn "."), the dialog is centered on the screen as before.


fvogel added on 2023-01-22 21:18:13:

Oh, and here is the test case:

package require Tk
tk_dialog .d hello "my test" questhead 0 OK Cancel


fvogel added on 2023-01-22 21:16:56:

I think you are talking about platforms like Linux that do not instantiate a native dialog but a Tk_made one.

The following patch makes tk_dialog behave as tk_messageBox, fontchooser, tk_chooseDirectory, tk_chooseColor:

Index: library/dialog.tcl
==================================================================
--- library/dialog.tcl
+++ library/dialog.tcl
@@ -141,11 +141,11 @@

     # 6. Withdraw the window, then update all the geometry information
     # so we know how big it wants to be, then center the window in the
     # display (Motif style) and de-iconify it.

-    ::tk::PlaceWindow $w
+    ::tk::PlaceWindow $w widget [winfo parent $w]
     tkwait visibility $w

     # 7. Set a grab and claim the focus too.

     if {$default >= 0} {

However I'm a little bit hesitating to apply this patch to Tk because it looks like the current behavior is intentional. The comment in the above snippet says the window will be centered in the display since this is Motif-style. That said, we can probably make this change since Motif is long dead.