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

相关文章

对Pytorch中Tensor的各种池化操作解析

AdaptiveAvgPool1d(N) 对一个C*H*W的三维输入Tensor, 池化输出为C*H*N, 即按照H轴逐行对W轴平均池化 >>> a = torch...

详解python3中的真值测试

1. 真值测试 所谓真值测试,是指当一种类型对象出现在if或者while条件语句中时,对象值表现为True或者False。弄清楚各种情况下的真值对我们编写程序有重要的意义。 对于一个对...

python opencv之分水岭算法示例

python opencv之分水岭算法示例

本文介绍了python opencv之分水岭算法示例,分享给大家,具体如下: 目标 使用分水岭算法对基于标记的图像进行分割 使用函数cv2.watershed() 原理:...

python算法学习之桶排序算法实例(分块排序)

复制代码 代码如下:# -*- coding: utf-8 -*- def insertion_sort(A):    """插入排序,作为桶排序的子排序"...

对python产生随机的二维数组实例详解

最近找遍了python的各个函数发现无法直接生成随机的二维数组,其中包括random()相关的各种方法,都没有得到想要的结果。最后在一篇博客中受到启发,通过列表解析的方法得到随机的二维数...