基于python的Tkinter實現(xiàn)一個簡易計算器
本文實例介紹了基于python的Tkinter實現(xiàn)簡易計算器的詳細代碼,分享給大家供大家參考,具體內容如下
第一種:使用python 的 Tkinter實現(xiàn)一個簡易計算器
#coding:utf-8
from Tkinter import *
import time
root = Tk()
def cacl(input_str):
if "x" in input_str:
ret = input_str.split("x")
return int(ret[0]) * int(ret[1])
def callback(n):
print n
def callback1(n):
print n
class App:
def __init__(self, master):
frame1 = Frame(master)
frame1.pack()
frame = Frame(master)
frame.pack()
Button(frame, text="1",command=lambda: callback(1) ).grid(row=0,column=0)
Button(frame, text="2",command=lambda: callback(2) ).grid(row=0,column=1)
Button(frame, text="3",command=lambda: callback(3) ).grid(row=0,column=2)
Button(frame, text="4",command=lambda: callback(4) ).grid(row=1,column=0)
Button(frame, text="5",command=lambda: callback(5) ).grid(row=1,column=1)
Button(frame, text="6",command=lambda: callback(6) ).grid(row=1,column=2)
Button(frame, text="7",command=lambda: callback(7) ).grid(row=2,column=0)
Button(frame, text="8",command=lambda: callback(8) ).grid(row=2,column=1)
Button(frame, text="9",command=lambda: callback(9) ).grid(row=2,column=2)
Button(frame, text="0",command=lambda: callback(0) ).grid(row=3,column=0)
Button(frame, text="+",command=lambda: callback1("+") ).grid(row=3,column=1)
Button(frame, text="-",command=lambda: callback1("-") ).grid(row=3,column=2)
Button(frame, text="*",command=lambda: callback1("*") ).grid(row=4,column=1)
Button(frame, text="/",command=lambda: callback1("/") ).grid(row=4,column=2)
Button(frame, text="=", command=self.say_hi).grid(row=4,column=0)
w = Label(frame1,text="輸入結果")
w.pack()
self.e = Entry(frame1)
self.e.pack(padx=5)
w1 = Label(frame1,text="計算結果")
w1.pack()
v = StringVar()
e1 = Entry(frame1, textvariable=v)
v.set("")
self.v = v
e1.pack()
def say_hi(self):
print "hi there, everyone!",self.e.get()
input_str = self.e.get()
self.v.set(cacl(input_str))
app = App(root)
root.mainloop()
第二種:基于Tkinter用50行Python代碼實現(xiàn)簡易計算器
Tkinter一般是python自帶的,所以代碼不需要其他組件,本程序是在python2.7版本實現(xiàn)的。
主要涉及了tkinter的使用,函數(shù)定義和調用,匿名函數(shù)的使用,類成員函數(shù)定義等python基礎知識,適合新手學習。
代碼如下:
from Tkinter import *
#創(chuàng)建橫條型框架
def frame(root, side):
w = Frame(root)
w.pack(side = side, expand = YES, fill = BOTH)
return w
#創(chuàng)建按鈕
def button(root, side, text, command = None):
w = Button(root, text = text, command = command)
w.pack(side = side, expand = YES, fill = BOTH)
return w
#繼承了Frame類,初始化程序界面的布局
class Calculator(Frame):
def __init__(self):
Frame.__init__(self)
self.pack(expand = YES, fill = BOTH)
self.master.title('Simple Calculater')
display = StringVar()
#添加輸入框
Entry(self, relief = SUNKEN,
textvariable = display).pack(side = TOP, expand = YES,
fill = BOTH)
#添加橫條型框架以及里面的按鈕
for key in('123', '456', '789', '-0.'):
keyF = frame(self, TOP)
for char in key:
button(keyF, LEFT, char, lambda w = display, c = char:w.set(w.get() + c))
#添加操作符按鈕
opsF = frame(self, TOP)
for char in '+-*/=':
if char == '=':
btn = button(opsF, LEFT, char)
btn.bind('<ButtonRelease - 1>', lambda e, s = self, w = display:s.calc(w), '+')
else:
btn = button(opsF, LEFT, char, lambda w = display, s = '%s' %char:w.set(w.get() + s))
#添加清除按鈕
clearF = frame(self, BOTTOM)
button(clearF, LEFT, 'clear', lambda w = display:w.set(''))
#調用eval函數(shù)計算表達式的值
def calc(self, display):
try:
display.set(eval(display.get()))
except:
display.set("ERROR")
#程序的入口
if __name__ == '__main__':
print('ok')
Calculator().mainloop()
實現(xiàn)效果如下圖:

關于計算器的精彩文章請查看《計算器專題》 ,更多精彩等你來發(fā)現(xiàn)!
以上就是本文的全部內容,希望對大家的學習Python程序設計有所幫助。
相關文章
Python并行編程多線程鎖機制Lock與RLock實現(xiàn)線程同步
這篇文章主要為大家介紹了Python并行編程多線程鎖機制Lock與RLock實現(xiàn)線程同步示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07
Appium+Python+pytest自動化測試框架的實戰(zhàn)
本文主要介紹了Appium+Python+pytest自動化測試框架的實戰(zhàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
Python re 模塊findall() 函數(shù)返回值展現(xiàn)方式解析
這篇文章主要介紹了Python re 模塊findall() 函數(shù)返回值展現(xiàn)方式解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08

