Python Tksimpledialog Positioning Next To The Root Window
I am trying to open a simple dialog window, in which the user enters a choice based on a menu presented on the root window. When I run the code however the dialog opens directly ab
Solution 1:
One way is to use a Toplevel widget. And it's not messy at all. Anyway, to place your input frame where you want, you have first to set dimensions and a position of your main (root) frame:
from Tkinter import *
import ttk
root = Tk()
root.lift()
w =300; h =200; x =10; y =10
root.geometry('%dx%d+%d+%d'% (w, h, x, y))
Label(root, text = "Menu Choices:").grid(row=1, column=0)
Label(root, text='1. Baloney and cheese').grid(row=2, column=0, pady=4)
Label(root, text='2. Roast chicken and gravy').grid(row=3, column=0, pady=4)
Label(root, text='3. Pear salad').grid(row=4, column=0, pady=4)
Label(root, text='4. Cateloupe and brocoli soup').grid(row=5, column=0, pady=4)
def store_entry():
print "Entry stored as "+ent.get()
def exit_entry():
print "Entry cancelled"
top.destroy()
top = Toplevel()
top.title('Franks Restaurant')
top.geometry("%dx%d+%d+%d" % (w, h, w+x+20, y))
Label(top, text='Please choose your meal').place(x=10,y=10)
ent = Entry(top); ent.place(x=10, y=50); ent.focus()
Button(top, text="OK", command=store_entry).place(x=10, y=150)
Button(top, text="Cancel", command=exit_entry).place(x=60, y=150)
root.mainloop()
This is an example of having user input window positioned where you want. You need to implement it for validation and storing of the user input and for as many users as you need.
Solution 2:
the Tk library actually has a function that does this although it is nominally 'private'. You can use it as follows.
import tkinter as tk
root = tk.Tk()
root.wm_geometry("800x600")
dialog = tk.Toplevel(root)
root_name = root.winfo_pathname(root.winfo_id())
dialog_name = dialog.winfo_pathname(dialog.winfo_id())
root.tk.eval('tk::PlaceWindow {0} widget {1}'.format(dialog_name, root_name))
root.mainloop()
This will place your dialog centred over the specified window (in this case th root window).
Solution 3:
- Here is an example of positioning a dialog relative to the main window.
- Each time you open the dialog it will track the root window position.
- it uses the geometry to set the left and top position of the dialog
'
import tkinter as tk
try: # allows the text to be more crisp on a high dpi displayfrom ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)
except:
pass# ----------------------------------------------------------------------------classCustomDialog:
def__init__(self, parent):
self.top = tk.Toplevel(parent)
xPos = root.winfo_x()
yPos = root.winfo_y()
posStr = "+" + str(xPos + 160) + "+" + str(yPos)
self.top.geometry(posStr)
tk.Label(self.top, text="Dlg is positioned relative to root window.").pack()
tk.Button(self.top, text="Ok", command=self.Ok).pack()
defOk(self):
self.top.destroy()
# ----------------------------------------------------------------------------defonOpenDlg():
inputDialog = CustomDialog(root)
root.wait_window(inputDialog.top)
# ----------------------------------------------------------------------------
root = tk.Tk()
tk.Label(root, text="Position Dlg Example").pack()
tk.Button(root, text="Open Dlg", command=onOpenDlg).pack()
root.mainloop()
'
Post a Comment for "Python Tksimpledialog Positioning Next To The Root Window"