Python Tkinter實現(xiàn)簡易計算器功能
更新時間:2018年01月30日 09:11:04 作者:Cullenyy
這篇文章主要為大家詳細介紹了Python Tkinter實現(xiàn)簡易計算器功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
閑暇時間用tkinter寫了個簡易計算器,可實現(xiàn)簡單的加減乘除運算,用了Button和Entry2個控件,下面是代碼,只是簡單的用了偏函數(shù)partial,因為那么多button的大部分參數(shù)都是一樣的,使用偏函數(shù)可以簡化參數(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()
下面是運行結果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Python?tkinter實現(xiàn)計算器功能
- 如何利用python的tkinter實現(xiàn)一個簡單的計算器
- Python tkinter實現(xiàn)簡單加法計算器代碼實例
- Python+tkinter使用40行代碼實現(xiàn)計算器功能
- python使用tkinter實現(xiàn)簡單計算器
- Python+tkinter使用80行代碼實現(xiàn)一個計算器實例
- 利用Tkinter(python3.6)實現(xiàn)一個簡單計算器
- Python編程使用tkinter模塊實現(xiàn)計算器軟件完整代碼示例
- 基于python的Tkinter實現(xiàn)一個簡易計算器
- python tkinter實現(xiàn)簡單計算器功能
相關文章
python 申請內存空間,用于創(chuàng)建多維數(shù)組的實例
今天小編就為大家分享一篇python 申請內存空間,用于創(chuàng)建多維數(shù)組的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python中Yield的基本用法及Yield與return的區(qū)別解析
Python中有一個非常有用的語法叫做生成器,用到的關鍵字就是yield,這篇文章主要介紹了Python中Yield的基本用法及Yield與return的區(qū)別,需要的朋友可以參考下2022-10-10
Python爬蟲實現(xiàn)使用beautifulSoup4爬取名言網(wǎng)功能案例
這篇文章主要介紹了Python爬蟲實現(xiàn)使用beautifulSoup4爬取名言網(wǎng)功能,結合實例形式分析了Python基于beautifulSoup4模塊爬取名言網(wǎng)并存入MySQL數(shù)據(jù)庫相關操作技巧,需要的朋友可以參考下2019-09-09

