在Tkinter中获取用户的输入

Icy*_*ame 7 python tkinter

目标

  1. 为用户提供文本字段.
  2. 一旦他按下按钮下面的按钮进入外壳,就打印他在字段中输入的文本.

这怎么可能使用Tkinter?

Linux Mint 14上的Specs Python 2.7

Icy*_*ame 8

我们将使用Entry小部件从用户处获取文本.然后我们将定义一个函数,它将从这个小部件中提取文本并在shell中打印出来.

def printtext():
    global e
    string = e.get() 
    print string   

from Tkinter import *
root = Tk()

root.title('Name')

e = Entry(root)
e.pack()
e.focus_set()

b = Button(root,text='okay',command=printtext)
b.pack(side='bottom')
root.mainloop()
Run Code Online (Sandbox Code Playgroud)

首先创建窗口实例.然后一个Entry小部件被打包.之后,另一个按钮小部件也被打包.该focus_set方法确保在运行时键盘焦点位于文本字段上.当按下按钮时,它将转到该功能,该功能将Entry使用该get方法从小部件中提取文本.

您可以Entry在此处找到有关小部件及其方法的更多信息:

入口小工具