Python3 's Tkinter write a simple little program

Source: Internet
Author: User
Tags base64 base64 encode

First, this semester began to learn python, but looked at Python2 and Python3, and finally chose the Python3

In the spirit of familiarity with Python, and also want to do some small programs to increase their proficiency in python. So write a simple program, this small program to achieve the BASA64, base32 the addition of decoding. And added a MD5-generated feature. PS: I think Python development is also very fun ...

Second, run the program:

The above is the overall interface of the program.

Third, the design of the program:

The source code is mapped below, and the required documents can be--------------search .....

ImportOSImportSYSImportBase64ImportHashlibImportTkinter as TKdefMain (): Root=tk. Tk ()//Here#Fix the root window sizeRoot.minsize (380, 530) Root.maxsize (380, 530)//Here the main is to control the size of the window, so that the window size can not be changed#root.geometry ("1366x250")Root.title ('Bin_lmmh_c Decoder')//Set the title of the main window#Display the Quit buttonText =edit (Root)//L= Tk. Label (Root, text='My name is Bin_lmmh_c', fg=' White', bg='Black', width=30) L.grid (Sticky=tk. E+tk. W+tk. S+tk. N) button (root, text)#Quitbutton (Root)Root.mainloop ()//The loop that enters the top-level window here
#Build the Edit and result Text, build two edit controlsdefedit (Root): Edit= Tk. Text (Root, fg=' White', bg='Green', font='Microsoft Ya-Black', width=30, height=10,) edit.grid (sticky=tk. N+tk. E+tk. W)#button passing parameters using lambda functions    #Delete all the value in the text editorClear1 = Tk. Button (Root, text='Clear', width=27, bg='Yellow', font='Microsoft Ya-Black', command=Lambda: Edit.delete (1.0, Tk. END) Clear1.grid () result= Tk. Text (Root, fg=' White', bg='Green', font='Microsoft Ya-Black', width=30, height=10,) result.grid (sticky=tk. N+tk. E+tk. W)#button passing parameters using lambda functions    #Delete all the value in the text editorClear2 = Tk. Button (Root, text='Clear', width=27, bg='Yellow', font='Microsoft Ya-Black', command=Lambda: Result.delete (1.0, Tk. END) Clear2.grid () text=[Edit, result]returnText
This defines all the button controls in the window, displays them, and sets the response function for each button, using the button's command option to controldefbutton (root, Text): Clu=0 B64en= Tk. Button (Root, text='Base64 Encode', fg=' White', bg='Green', command=Lambda: B64encode (text)) B64de= Tk. Button (Root, text='Base64 Decode', fg=' White', bg='Green', command=Lambda: B64decode (text)) B32en= Tk. Button (Root, text='Base32 Encode', fg=' White', bg='Green', command=Lambda: B32encode (text)) B32de= Tk. Button (Root, text='Base32 Decode', fg=' White', bg='Green', command=Lambda: B32encode (text)) Md5do= Tk. Button (Root, text='-md5 creator-', fg=' White', bg='Green', command=Lambda: Md5create (text)) but=[B64en, B64de, B32en, B32de, Md5do] forIinchBut:i.grid (Row=CLU, Column=1, STICKY=TK. N+tk. S+tk. E+tk. W) CLU+ = 1return butdefB64encode (text):#print (len (text), text)Edit, result = Text[0], text[1] Enc= Edit.get (1.0, Tk. END)Try: Res= Base64.b64encode (Enc[0:-1].encode ('ASCII')) #print ("res =", res) except: returnFalse Result.insert (1.0, Res.decode ('ASCII')) returnTruedefB64decode (text):#print (len (text), text)Edit, result = Text[0], text[1] Dec= Edit.get (1.0, Tk. END)Try: Res= Base64.b64decode (Dec[0:-1].encode ('ASCII')) except: returnFalse Result.insert (1.0, Res.decode ('ASCII')) returnTruedefB32encode (text):#print (len (text), text)Edit, result = Text[0], text[1] Enc= Edit.get (1.0, Tk. END)Try: Res= Base64.b32encode (Enc[0:-1].encode ('ASCII')) Result.insert (1.0, Res.decode ('ASCII')) except: returnFalse#showmessage (None, ' Something Error ') returnTrue
This defines the MD5 generation functiondefmd5create (text):#print (len (text), text)Edit, result = Text[0], text[1] Dec= Edit.get (1.0, Tk. END)//Get the contents of the edit control#print ("Len Dec =", Len (Dec[0:-1])) #print ("Dec =", dec)res =hashlib.md5 ()Try: #It'll add a new line characterRes.update (Dec[0:-1].encode ('ASCII')) except: returnFalse Result.insert (1.0, Res.hexdigest ())//Insert the data after MD5 into the output edit controlreturnTrueif __name__=='__main__': Main ()

Iv. Simple Self-summary:

Before the production of this gadget, encountered some problems and some small tricks, such as the button to pass the parameters of the problem, you can use the expression of lambda easy to complete, and not to consider what the global variables and the like, this I feel is very convenient, the problems encountered by the network to solve, It feels good, but I'm very uncomfortable with the bad habit of not documenting my problems, and I'm going to change this bad habit.    Study hard and make Progress day by day. PS: Sharing is very happy

Python3 's Tkinter write a simple little program

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.