
encryptgui.py 的程式原始碼如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | from tkinter import * from encrypt import Encrypt import os class EncryptGUI(Frame): def __init__( self , master = None ): Frame.__init__( self , master) self .grid() self .createWidgets() self .e = None self .userinput = "" self .result = "" def createWidgets( self ): self .inputText = Label( self ) self .inputText[ "text" ] = "Input:" self .inputText.grid(row = 0 , column = 0 ) self .inputField = Entry( self ) self .inputField[ "width" ] = 50 self .inputField.grid(row = 0 , column = 1 , columnspan = 6 ) self .outputText = Label( self ) self .outputText[ "text" ] = "Output:" self .outputText.grid(row = 1 , column = 0 ) self .outputField = Entry( self ) self .outputField[ "width" ] = 50 self .outputField.grid(row = 1 , column = 1 , columnspan = 6 ) self .new = Button( self ) self .new[ "text" ] = "New" self .new.grid(row = 2 , column = 0 ) self .new[ "command" ] = self .newMethod self .load = Button( self ) self .load[ "text" ] = "Load" self .load.grid(row = 2 , column = 1 ) self .load[ "command" ] = self .loadMethod self .save = Button( self ) self .save[ "text" ] = "Save" self .save.grid(row = 2 , column = 2 ) self .save[ "command" ] = self .saveMethod self .encode = Button( self ) self .encode[ "text" ] = "Encode" self .encode.grid(row = 2 , column = 3 ) self .encode[ "command" ] = self .encodeMethod self .decode = Button( self ) self .decode[ "text" ] = "Decode" self .decode.grid(row = 2 , column = 4 ) self .decode[ "command" ] = self .decodeMethod self .clear = Button( self ) self .clear[ "text" ] = "Clear" self .clear.grid(row = 2 , column = 5 ) self .clear[ "command" ] = self .clearMethod self .copy = Button( self ) self .copy[ "text" ] = "Copy" self .copy.grid(row = 2 , column = 6 ) self .copy[ "command" ] = self .copyMethod self .displayText = Label( self ) self .displayText[ "text" ] = "something happened" self .displayText.grid(row = 3 , column = 0 , columnspan = 7 ) def newMethod( self ): self .e = Encrypt() self .displayText[ "text" ] = self .e def loadMethod( self ): if os.path.exists( "./code.txt" ): f = open ( './code.txt' , 'r' ) code = f.readline() self .e = Encrypt() self .e.setCode(code) self .displayText[ "text" ] = "code: " + self .e.getCode() else : self .displayText[ "text" ] = "Load denied!!" def saveMethod( self ): if self .e = = None : self .displayText[ "text" ] = "No Encrypt object can save!!" else : f = open ( './code.txt' , 'w' ) f.write( self .e.getCode()) f.closed self .displayText[ "text" ] = "The code is saved." def encodeMethod( self ): self .userinput = self .inputField.get() if self .userinput = = "": self .displayText[ "text" ] = "No input string!!" else : if self .e = = None : self .displayText[ "text" ] = "No encrypt object!!" else : self .result = self .e.toEncode( self .userinput) self .outputField.delete( 0 , 200 ) self .outputField.insert( 0 , self .result) self .displayText[ "text" ] = "Encoding success!!" def decodeMethod( self ): self .userinput = self .inputField.get() if self .userinput = = "": self .displayText[ "text" ] = "No input string!!" else : if self .e = = None : self .displayText[ "text" ] = "No encrypt object!!" else : self .result = self .e.toDecode( self .userinput) self .outputField.delete( 0 , 200 ) self .outputField.insert( 0 , self .result) self .displayText[ "text" ] = "Decoding success!!" def clearMethod( self ): self .e = None self .userinput = "" self .result = "" self .inputField.delete( 0 , 200 ) self .outputField.delete( 0 , 200 ) self .displayText[ "text" ] = "It's done." def copyMethod( self ): if self .result = = "": self .displayText[ "text" ] = "Copy denied!!" else : self .clipboard_clear() self .clipboard_append( self .result) self .displayText[ "text" ] = "It is already copied to the clipboard." if __name__ = = '__main__' : root = Tk() app = EncryptGUI(master = root) app.mainloop() # 《程式語言教學誌》的範例程式 # 檔名:encryptgui.py # 功能:示範 Python 程式 # 作者:張凱慶 # 時間:西元 2012 年 12 月 |
您可以繼續參考
範例程式碼
相關目錄
回 Python 入門指南
回 Python 教材
回首頁
參考資料
http://www.python.org/
http://docs.python.org/3.2/tutorial/index.html
http://docs.python.org/3.2/library/index.html
http://www.tutorialspoint.com/python/index.htm
http://interactivepython.org/courselib/static/thinkcspy/index.html
http://interactivepython.org/courselib/static/pythonds/index.html
http://en.wikibooks.org/wiki/Python_Programming
http://jwork.org/learn/doc/doku.php?id=python:start
沒有留言:
張貼留言