国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

基于python的Tkinter實現一個簡易計算器

2019-11-25 16:59:59
字體:
來源:轉載
供稿:網友

本文實例介紹了基于python的Tkinter實現簡易計算器的詳細代碼,分享給大家供大家參考,具體內容如下

第一種:使用python 的 Tkinter實現一個簡易計算器

#coding:utf-8from Tkinter import *import timeroot = 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 ndef callback1(n):print nclass 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 = ve1.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代碼實現簡易計算器
 Tkinter一般是python自帶的,所以代碼不需要其他組件,本程序是在python2.7版本實現的。

 主要涉及了tkinter的使用,函數定義和調用,匿名函數的使用,類成員函數定義等python基礎知識,適合新手學習。

代碼如下:

from Tkinter import *  #創建橫條型框架 def frame(root, side):  w = Frame(root)  w.pack(side = side, expand = YES, fill = BOTH)  return w #創建按鈕 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函數計算表達式的值  def calc(self, display):  try:  display.set(eval(display.get()))  except:  display.set("ERROR") #程序的入口 if __name__ == '__main__':  print('ok')  Calculator().mainloop() 

實現效果如下圖:

以上就是本文的全部內容,希望對大家的學習Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 苏州市| 泰顺县| 神农架林区| 徐汇区| 田东县| 定南县| 琼结县| 江都市| 汶川县| 金昌市| 偃师市| 黄骅市| 通辽市| 镇赉县| 巴彦淖尔市| 宣武区| 丹阳市| 手机| 孟连| 潞城市| 南开区| 裕民县| 贵阳市| 桑植县| 霍山县| 舟曲县| 阳江市| 黔东| 阳西县| 古浪县| 界首市| 平山县| 额济纳旗| 遵义县| 正宁县| 麦盖提县| 仙居县| 邵武市| 泰宁县| 进贤县| 衡水市|