Python使用tkinter库实现文本显示用户输入功能示例

yipeiwu_com5年前Python基础

本文实例讲述了Python使用tkinter库实现文本显示用户输入功能。分享给大家供大家参考,具体如下:

#coding:utf-8
from Tkinter import *
class App:
  def __init__(self,root):
    #定义帧
    frame = Frame(root)
    frame.pack()
    self.frame = frame
    w = Label(frame,text = "calculator")
    w.pack()
    self.newinput()
    #调用回调函数
    button1 = Button(frame,text='1',fg="red",command = lambda : self.buttoncb(1))
    button1.pack()
    button2 = Button(frame,text='2',fg="red",command = lambda : self.buttoncb(2))
    button2.pack()
    button = Button(frame,text='Quit',fg="red",command = root.quit)
    button.pack()
  def newinput(self):
    v = StringVar()
    e = Entry(self.frame,textvariable = v)
    self.v = v
    e.pack()
  #定义回调函数
  def buttoncb(self,i):
    #print "button"
    val = self.v.get()
    self.v.set(val+str(i))
root=Tk()
a = App(root)
root.mainloop()

运行结果:

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

详解Python中 sys.argv[]的用法简明解释

详解Python中 sys.argv[]的用法简明解释

因为是看书自学的python,开始后不久就遇到了这个引入的模块函数,且一直在IDLE上编辑了后运行,试图从结果发现它的用途,然而结果一直都是没结果,也在网上查了许多,但发现这个问题的比较...

详解python读取image

python 读取image 在python中我们有两个库可以处理图像文件,scipy和matplotlib. 安装库 pip install matplotlib pillow s...

win7+Python3.5下scrapy的安装方法

win7+Python3.5下scrapy的安装方法

如何在win7+Python3.5的环境下安装成功scrapy? 通过pip3 install Scrapy直接安装,一般会报错:error: Unable to find vcvars...

通过python实现windows桌面截图代码实例

这篇文章主要介绍了python实现windows桌面截图代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码实例 impo...

python通过BF算法实现关键词匹配的方法

本文实例讲述了python通过BF算法实现关键词匹配的方法。分享给大家供大家参考。具体实现方法如下: 复制代码 代码如下:#!/usr/bin/python # -*- coding:...