Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Tkinter in Jupyter Notebook

Tags:

python

tkinter

I have just started using Tkinter and trying to create a simple pop-up box in python. I have copy pasted a simple code from a website:

from Tkinter import *

master = Tk()
Label(master, text="First Name").grid(row=0)
Label(master, text="Last Name").grid(row=1)

e1 = Entry(master)
e2 = Entry(master)

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)

mainloop( )

This code is taking really long time to run, it has been almost 5 minutes! Is it not possible to just run this snippet? Can anybody tell me how to use Tkinter?

I am using jupyter notebook and python version 2.7. I would request a solution for this version only.

like image 285
sky_bird Avatar asked Aug 29 '17 09:08

sky_bird


People also ask

Can I run tkinter in Jupyter notebook?

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. Now, after verifying the installation, you are ready to write your Tkinter application code in Jupyter notebook.

Can I use tkinter in Anaconda?

Popular Python distributions like ActivePython and Anaconda both come with Tkinter. The simplest method to install Tkinter in a Windows environment is to download and install ActivePython 3.8 from here.

Can we run tkinter in Colab?

You will have to run Python on your desktop or laptop to use tkinter . Additionally, the colab environment is a form of IPython notebook, which is not really a standard Python environment. I would not recommend trying to run tkinter programs from an IPython notebook even if you have IPython running locally.


1 Answers

from Tkinter import *

def printData(firstName, lastName):
    print(firstName)
    print(lastName)
    root.destroy()

def get_input():

    firstName = entry1.get()
    lastName = entry2.get()
    printData(firstName, lastName)


root = Tk()
#Label 1
label1 = Label(root,text = 'First Name')
label1.pack()
label1.config(justify = CENTER)

entry1 = Entry(root, width = 30)
entry1.pack()

label3 = Label(root, text="Last Name")
label3.pack()
label1.config(justify = CENTER)

entry2 = Entry(root, width = 30)
entry2.pack()

button1 = Button(root, text = 'submit')
button1.pack() 
button1.config(command = get_input)

root.mainloop()

Copy paste the above code into a editor, save it and run using the command,

python sample.py

Note: The above code is very vague. Have written it in that way for you to understand.

like image 83
DeathJack Avatar answered Sep 21 '22 17:09

DeathJack