I am creating a pop-up window using Tkinter.
def show_warning(self, msg):
self.warning = tk.Toplevel()
self.centrify_widget(self.warning)
self.warning.title('Warning!')
self.warn.resizable(0, 0)
self.warnFr = ttk.Frame(self.warning, borderwidth=2, relief='groove')
self.warnFr.grid()
ttk.Label(self.warnFr, font='TkDefaultFont 11', text=msg).grid()
ttk.Button(self.warnFr, text='OK', command=self.warning.destroy).grid()
I want this pop-up window to appear centrified according to the application position.
def centrify_widget(self, widget):
rwidth = self.winfo_screenwidth()
rheight = self.winfo_screenheight()
x, y = (rwidth/2) - (widget.width/2), (rheight/2) - (widget.height/2)
widget.geometry('%dx%d+%d+%d' % (widget.width, widget.height, x, y))
The code above won't work because tk.Toplevel
does not have width or height attributes.
So, how do I centrify the pop-up window when I do not know its height and width?
I can't hardcode it because my show_warning
function accepts msg
of different length and if I explicitly give height and width the window geometry will be broken like below.