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

yipeiwu_com7年前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程序设计有所帮助。

相关文章

在Pycharm中执行scrapy命令的方法

在Pycharm中执行scrapy命令的方法

当你检查scrapy二进制文件时,你会注意到这么一段python script #!/usr/bin/python from scrapy.cmdline import execu...

在Linux上安装Python的Flask框架和创建第一个app实例的教程

在Linux上安装Python的Flask框架和创建第一个app实例的教程

无论你在linux上娱乐还是工作,这对你而言都是一个使用python来编程的很好的机会。回到大学我希望他们教我的是Python而不是Java,这学起来很有趣且在实际的应用如yum包管理器...

Python画柱状统计图操作示例【基于matplotlib库】

Python画柱状统计图操作示例【基于matplotlib库】

本文实例讲述了Python画柱状统计图操作。分享给大家供大家参考,具体如下: 一、工具:python的matplotlib.pyplot 库 二、案例: import matplot...

在pycharm中使用git版本管理以及同步github的方法

在pycharm中使用git版本管理以及同步github的方法

注意:首先你电脑必须安装git版本控制器(软件),在官网下载即可。 pycharm中使用git以及github很简单,首先在设置中搜索github: 点击右边的Create API T...

Python画图高斯分布的示例

如下所示: import matplotlib.pyplot as plt import numpy as np import math def gaussian(sigma, x,...