Python:ui Programming Calculator

Source: Internet
Author: User
Tags integer division

Origin:

Studying Python UI programming, I prefer its native component, so I learn tkinter, TTK component usage. Find a calculator open source code, slightly trimming, think of memo.
Its interface:

1. Source code (Python 2.7):

#Encoding:utf-8 fromTkinterImport* fromTtkImport*Calc=Tk () calc.title ('Calculator') Calc.resizable (False, False) buttons= [    '7','8','9','*','C',    '4','5','6','/','Neg',    '1','2','3','-','$',    '0','.','=','+','@']#Set up GUIrow = 1Col=0style=Style () style.configure ('BW. TButton', padding=12) forIinchbuttons:action=Lambdax=i:click_event (x) Button (calc, text=i, Width=7, Command=action, style='BW. TButton'). Grid (Row=row, Column=col, sticky='NESW',) Col+ = 1ifCol > 4: Col=0 Row+ = 1Display= Entry (Calc, width=60) Display.grid (Row=0, Column=0, columnspan=5) calc.update () W=calc.winfo_reqwidth () H=calc.winfo_reqheight () S_w=calc.winfo_screenwidth () S_h=calc.winfo_screenheight () calc.geometry ('%dx%d+%d+%d'% (W, H, (s_w-w)/2, (S_H-H)/2) ) Display.focus_set ()defclick_event (key):#=-Calculate results    ifKey = ='=':        #Safeguard against integer division        if '/' inchDisplay.get () and '.'  not inchdisplay.get (): Display.insert (END,'. 0')        #attempt to evaluate results        Try: Result=eval (display.get ()) Display.insert (END,' = '+Str (result))except: Display.insert (END,'Error, use only valid chars')            #clear Display, C    elifKey = ='C': Display.delete (0, END)#$-Clear display    elifKey = ='$': Display.delete (0, end) Display.insert (end,'$$$ $C. $R. $E $A. $M. $')    #@, clear display    elifKey = ='@': Display.delete (0, end) Display.insert (end,'website')    #neg-Negate term    elifKey = ='Neg':        if '=' inchdisplay.get (): Display.delete (0, END)Try:            ifDisplay.get () [0] = ='-': Display.delete (0)Else: Display.insert (0,'-')        exceptIndexerror:Pass            #clear display and start new input    Else:        if '=' inchdisplay.get (): Display.delete (0, end) Display.insert (end, key)#RUNTIMECalc.mainloop ()

2. Generate EXE

Repeatedly compared Py2exe and pyinstaller, found that py2exe in the x64 bit can not support the generation of an EXE file, and its x32, tkinter, can not generate a file.

The effort, but also just less to generate a few files, very uncomfortable:

With Pyinstaller, a single file can be generated. But verifying its startup speed is time-consuming:

Comprehensive comparison, Python do UI, real non-convenient things, with its glue language advantages, enough!

Python:ui Programming Calculator

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.