在 Tkinter 中接受用户的输入 [英] Taking input from the user in Tkinter

查看:40
本文介绍了在 Tkinter 中接受用户的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标

  1. 给用户一个文本字段.
  2. 在他按下字段下方的按钮进入外壳后,打印他在字段中输入的文本.

这怎么可能使用 Tkinter?

How is this possible using Tkinter?

规格 Linux Mint 14 上的 Python 2.7

Specs Python 2.7 on Linux Mint 14

推荐答案

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

We will take the text from the user using the Entry widget. And then we will define a function that will extract the text from this widget and print it out in the 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()

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

The window instance is first created. And then an Entry widget is packed. After that another button widget is also packed. The focus_set method makes sure that the keyboard focus is on the text field when this is run. When the button is pressed it will go to the function which will extract the text from the Entry widget using the get method.

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

You can find more about the Entry widget and its methods here:

入口小工具

这篇关于在 Tkinter 中接受用户的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆