1

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.

enter image description here

minerals
  • 6,090
  • 17
  • 62
  • 107
  • 1
    have you checked `widget.winfo_height()` and `widget.winfo_width()`? – Lafexlos Feb 03 '16 at 20:21
  • 1
    This looks like a duplicate of http://stackoverflow.com/q/3352918/7432 – Bryan Oakley Feb 03 '16 at 20:52
  • Thing is, Toplevel window returns the resolution of my monitor 1600x900 but appears to be small enough to contain the message cause this is the default behaviour. If I attempt to set it via geometry the pop-up window will become 1600x900 in size. – minerals Feb 03 '16 at 21:44
  • actually the example in the answer link above works, hmm – minerals Feb 03 '16 at 21:48

0 Answers0