How to create a MacOS compatible pop-up window in Tkinter the right way.

Udit Vashisht
3 min readMar 19, 2018

--

This post will cover:-
1. How to use Classes to add reusability to your Tkinter app.
2. How to implement Pop-Ups in Tkinter apps for MacOS.

I, deliberately, won’t be going into the nitty gritty of window size, widget placement etc. as the essence of this post lies in the above two objectives.

Recently, I have started learning Python and It is an ocean full of gems. I decided to give GUI to one of my coding exercise. Tkinter is a wonderful library to turn your lines of code into true GUI apps. Yet, due to restrictions by MacOS and limitation in the Tkinter, I got struck at a very basic exercise of creating Popups for my application.

Tkinter provides you out-of-the-box message boxes, which pops up and have options like showinfo, showwarning, showerror, askquestion, askokcancel, askyesno, or askretrycancel. But, I assume that only these options can be limited and It might not go well with your app style if you require additional pop-ups in your app.

Same, was the case with me, I wanted my pop-ups to do more than what standard message boxes do. So, I sought the guidance of my master “Google” and within few seconds it threw an option to use Toplevel widget to implement pop-up windows. Supposedly, the code to be used was :-

Now, there are quite a few things wrong with the above code:-
1. It is not advisable to import everything from a module using import *. That certainly is a wrong practice, if you use a lot of third party modules as they tend to have methods with same name and that could result in conflicts in your code.
2. It is simple and you might consider it beautiful but it is not a pythonic way to do the stuff. What, if you want to have more than one pop-up windows in your app? Will you keep on repeating yourself and creating a new function for each pop-up window? That certainly will convert this simple and beautiful looking code into complex and ugly one.
3. Thirdly, this code didn’t open the popup window as pop-up for me (I am using MacOs High Sierra) instead it opened the popup window in a new tab. Have a look at it.

So, Now let us do it the right way :-

First of all we will create the main frame and the root window using a Class object:-

This will give us the following window:-

Since, we have not provided any handler for the button, it will do nothing. Let’s add a function for the button in the main Class

So, now we have added a pop_up function, which will withdraw the root window i.e. the main frame and show the popUp class. Let us now define the popUp class:-

So, we have created popUp which is a class of tk.Toplevel and in the init function, we have initiated the Toplevel class of Tkinter. self.transient(root) will move the root window to the background, and self.lift() will bring the popup in front window. Now, let us implement the close button on it :-

Hitting the “Close” button will destroy the self i.e. popup and update and deiconify the root window, bringing it back to the front. So, the complete code is as under:-

This is how it works in real :-

So, it has worked like a charm. Hope you have liked it. Feel free to post any questions.

--

--

Udit Vashisht

I am a tech enthusiast and a Python Learner. I love to share my experiences with others on medium.