[It learning] reprint Python Project calculator

Source: Internet
Author: User

This is the calculator from the web search for small Python projects (original address: http://www.2cto.com/kf/201402/279637.html). However, this code is estimated to be written in Python 2.

If you are using a Python version of 3 or more, you need to install pillow yourself. Windows can use the PIP install Pillow on the command line.

However, when referenced in the program, the name of the pillow is still pil. See the example below.

Thinking

1, Tkinter programming to consider what aspects?

2. What are the imperfections of this program? For example, enter 9+-2 to see what the result is?

3, the calculator in the computer to take a look, compared to what can be improved?

4. What are the advantages of grid mode layout and what are the disadvantages?

# -*-coding:utf-8-*- # Author:cullen
#原来的代码中有这个作者信息, you can search by "Cullent python", not sure whether the same person
#import the Modulefrom tkinter import *importTkinter.font as TkfontimportOsfrom Functools ImportPartialfrom PIL ImportImage, Imagetk defGet_input (Entry, Argu): Entry.insert (END, Argu) defBACKSPACE (entry): Input_len =Len (Entry.get ()) Entry.delete (input_len-1) defClear (Entry): Entry.delete (0, END) defCalc (entry): input =Entry.get () output =Str (eval (Input.strip ())) Clear (Entry) Entry.insert (END, Output) defCal (): 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< span>, 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< span>, Command=lambda:clear (entry), Activebackground =  button_active_bg) button16.grid (row=5, Column=1, pady=5< span>) button17 = Button (root, text= ' = ', BG=BUTTON_BG, padx=10, pady=3 , Command=lambda:calc (entry), Activebackgro und =  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 ()         

[It learning] reprint Python Project 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.