本文實例講述了Python使用tkinter庫實現文本顯示用戶輸入功能。分享給大家供大家參考,具體如下:
#coding:utf-8from 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程序設計有所幫助。
新聞熱點
疑難解答