Using Tkinter in Jupyter Notebook

Tkinter is a Python library used for creating and developing GUI-based applications. It is completely open-source which works on Windows, Mac, Linux, and Ubuntu. In Windows operating system, we can install the Tkinter library using the command pip install tkinter. It will install all the other modules that come with Tkinter library. Tkinter can be installed on Jupyter notebook as well, by using the command pip install tkinter. We can run all the standard commands of Tkinter in Jupyter notebook.

Once we have installed Tkinter in Jupyter notebook, then we can verify the installation by typing the following command −

from tkinter import *

Now, after verifying the installation, you are ready to write your Tkinter application code in Jupyter notebook. For example, type the following code in Jupyter notebook and run the code by pressing "Shift + Enter".


#Import tkinter library
from tkinter import *
#Create an instance of Tkinter frame or window
win= Tk()
#Set the geometry of tkinter frame
def callback():
   Label(win, text="Hello World!", font=('Century 20 bold')).pack(pady=4)
#Create a Label and a Button widget
btn=Button(win, text="Press Enter", command= callback)
win.bind('<Return>',lambda event:callback())


Running the above code will display a window that contains a button widget.

In the given output, when we press the Enter key, it will show the Label widgets with some text.