Python uses only 40 lines of code to write a calculator instance, and python40 lines

Source: Internet
Author: User

Python uses only 40 lines of code to write a calculator instance, and python40 lines

This example describes a calculator written in Python with only 40 lines of code. We will share this with you for your reference. The details are as follows:

:

Code:

From tkinter import * reset = Truedef buttonCallBack (event): global label global reset num = event. widget ['text'] if num = 'C': label ['text'] = "0" return if num in "= ": label ['text'] = str (eval (label ['text']) reset = True return s = label ['text'] if s = '0' or reset = True: s = "" reset = False label ['text'] = s + num # Main Window root = Tk () root. wm_title ("Calculator") # display column 1 label = Label (root, text = "0", background = "white", anchor = "e ") label ['width'] = 35 label ['height'] = 2label. grid (row = 1, columnspan = 4, sticky = W) # button showText = "789/456 * 123-0.C +" for I in range (4): for j in range (4 ): B = Button (root, text = showText [I * 4 + j], width = 7) B. grid (row = I + 2, column = j) B. bind ("<button-1>", buttonCallBack) showText = "()" for I in range (2): www.jb51.net B = Button (root, text = showText [I], width = 7) B. grid (row = 6, column = 2 + I) B. bind ("<button-1>", buttonCallBack) B = Button (root, text = "=") B. grid (row = 6, columnspan = 2, sticky = "we") B. bind ("<button-1>", buttonCallBack) root. mainloop () </button-1> </button-1> </button-1>

PS: Here are some recommended computing tools for your reference:

Calculation tool for online mona1 functions (equations:
Http://tools.jb51.net/jisuanqi/equ_jisuanqi

Scientific calculator online use _ advanced calculator online computing:
Http://tools.jb51.net/jisuanqi/jsqkexue

Online calculator _ standard calculator:
Http://tools.jb51.net/jisuanqi/jsq

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.