Answer a question

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.

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.

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐