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

首頁(yè) > 編程 > Python > 正文

Python Tkinter實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能

2020-02-22 23:02:25
字體:
供稿:網(wǎng)友

閑暇時(shí)間用tkinter寫了個(gè)簡(jiǎn)易計(jì)算器,可實(shí)現(xiàn)簡(jiǎn)單的加減乘除運(yùn)算,用了Button和Entry2個(gè)控件,下面是代碼,只是簡(jiǎn)單的用了偏函數(shù)partial,因?yàn)槟敲炊郻utton的大部分參數(shù)都是一樣的,使用偏函數(shù)可以簡(jiǎn)化參數(shù)傳遞,避免同樣的參數(shù)傳遞寫N次。

# -*- coding: utf-8 -*- #author: Cullen  #import the module from Tkinter import * import tkFont import os from functools import partial from PIL import Image, ImageTk  def get_input(entry, argu):   entry.insert(END, argu)  def backspace(entry):   input_len = len(entry.get())   entry.delete(input_len - 1)  def clear(entry):   entry.delete(0, END)  def calc(entry):   input = entry.get()   output = str(eval(input.strip()))   clear(entry)   entry.insert(END, output)  def cal():   root = Tk()   root.title("Calc")   root.resizable(0,0)    entry_font = tkFont.Font(size=12)   entry = Entry(root, justify="right", font=entry_font)   entry.grid(row=0, column=0, columnspan=4, sticky=N+W+S+E, padx=5, pady=5)    button_font = tkFont.Font(size=10, weight=tkFont.BOLD)   button_bg = '#D5E0EE'   button_active_bg = '#E5E35B'    myButton = partial(Button, root, bg=button_bg, padx=10, pady=3, activebackground = button_active_bg)    button7 = myButton(text='7', command=lambda : get_input(entry, '7'))   button7.grid(row=1, column=0, pady=5)    button8 = myButton(text='8', command=lambda : get_input(entry, '8'))   button8.grid(row=1, column=1, pady=5)    button9 = myButton(text='9', command=lambda : get_input(entry, '9'))   button9.grid(row=1, column=2, pady=5)    button10 = myButton(text='+', command=lambda : get_input(entry, '+'))   button10.grid(row=1, column=3, pady=5)    button4 = myButton(text='4', command=lambda : get_input(entry, '4'))   button4.grid(row=2, column=0, pady=5)    button5 = myButton(text='5', command=lambda : get_input(entry, '5'))   button5.grid(row=2, column=1, pady=5)    button6 = myButton(text='6', command=lambda : get_input(entry, '6'))   button6.grid(row=2, column=2, pady=5)    button11 = myButton(text='-', command=lambda : get_input(entry, '-'))   button11.grid(row=2, column=3, pady=5)    button1 = myButton(text='1', command=lambda : get_input(entry, '1'))   button1.grid(row=3, column=0, pady=5)    button2 = myButton(text='2', command=lambda : get_input(entry, '2'))   button2.grid(row=3, column=1, pady=5)    button3 = myButton(text='3', command=lambda : get_input(entry, '3'))   button3.grid(row=3, column=2, pady=5)    button12 = myButton(text='*', command=lambda : get_input(entry, '*'))   button12.grid(row=3, column=3, pady=5)    button0 = myButton(text='0', command=lambda : get_input(entry, '0'))   button0.grid(row=4, column=0, columnspan=2, padx=3, pady=5, sticky=N+S+E+W)    button13 = myButton(text='.', command=lambda : get_input(entry, '.'))   button13.grid(row=4, column=2, pady=5)    button14 = Button(root, text='/', bg=button_bg, padx=10, pady=3,            command=lambda : get_input(entry, '/'))   button14.grid(row=4, column=3, pady=5)    button15 = Button(root, text='<-', bg=button_bg, padx=10, pady=3,            command=lambda : backspace(entry), activebackground = button_active_bg)   button15.grid(row=5, column=0, pady=5)    button16 = Button(root, text='C', bg=button_bg, padx=10, pady=3,            command=lambda : clear(entry), activebackground = button_active_bg)   button16.grid(row=5, column=1, pady=5)    button17 = Button(root, text='=', bg=button_bg, padx=10, pady=3,            command=lambda : calc(entry), activebackground = button_active_bg)   button17.grid(row=5, column=2, columnspan=2, padx=3, pady=5, sticky=N+S+E+W)    root.mainloop()  if __name__ == '__main__':   cal()             
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 芮城县| 凤凰县| 常宁市| 德化县| 瑞丽市| 临颍县| 汉源县| 临安市| 阿拉尔市| 长寿区| 措勤县| 宜宾县| 夹江县| 双流县| 文登市| 来凤县| 六安市| 全椒县| 澳门| 武乡县| 中阳县| 新竹市| 米泉市| 合川市| 合肥市| 衡阳市| 金昌市| 五大连池市| 武胜县| 朝阳县| 孝昌县| 明溪县| 寿阳县| 嘉荫县| 海盐县| 郧西县| 武功县| 鹤岗市| 广州市| 三穗县| 林芝县|