在 Jupyter Notebook 中使用 Tkinter [英] Using Tkinter in Jupyter Notebook
问题描述
我刚刚开始使用 Tkinter 并尝试在 python 中创建一个简单的弹出框.我从网站上复制粘贴了一个简单的代码:
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( )
这段代码运行时间真的很长,已经快5分钟了!不能只运行这个片段吗?谁能告诉我如何使用 Tkinter?
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?
我使用的是 jupyter notebook 和 python 2.7 版.我只要求为这个版本提供解决方案.
I am using jupyter notebook and python version 2.7. I would request a solution for this version only.
推荐答案
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
注意:上面的代码很模糊.写出来让你看懂了.
这篇关于在 Jupyter Notebook 中使用 Tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!