001 | from tkinter import * |
002 | from tkinter.ttk import * |
003 | from encrypt import Encrypt |
004 | import os |
005 | |
006 | # Encrypt 的 GUI 類別 |
007 | class EncryptGUI(Frame): |
008 | # 設定初值 |
009 | def __init__(self, master=None): |
010 | Frame.__init__(self, master) |
011 | self.grid() |
012 | self.createWidgets() |
013 | self.e = None |
014 | self.userinput = "" |
015 | self.result = "" |
016 | |
017 | # 建立所有視窗元件 |
018 | def createWidgets(self): |
019 | self.it = Label(self) |
020 | self.it["text"] = "Input:" |
021 | self.it.grid(row=0, column=0) |
022 | self.ifd = Entry(self) |
023 | self.ifd["width"] = 60 |
024 | self.ifd.grid(row=0, column=1, |
025 | columnspan=6) |
026 | |
027 | self.ot = Label(self) |
028 | self.ot["text"] = "Output:" |
029 | self.ot.grid(row=1, column=0) |
030 | self.ofd = Entry(self) |
031 | self.ofd["width"] = 60 |
032 | self.ofd.grid(row=1, column=1, |
033 | columnspan=6) |
034 | |
035 | self.nb = Button(self) |
036 | self.nb["text"] = "New" |
037 | self.nb.grid(row=2, column=0) |
038 | self.nb["command"] = self.nm |
039 | self.lb = Button(self) |
040 | self.lb["text"] = "Load" |
041 | self.lb.grid(row=2, column=1) |
042 | self.lb["command"] = self.lm |
043 | self.sb = Button(self) |
044 | self.sb["text"] = "Save" |
045 | self.sb.grid(row=2, column=2) |
046 | self.sb["command"] = self.sm |
047 | self.eb = Button(self) |
048 | self.eb["text"] = "Encode" |
049 | self.eb.grid(row=2, column=3) |
050 | self.eb["command"] = self.em |
051 | self.db = Button(self) |
052 | self.db["text"] = "Decode" |
053 | self.db.grid(row=2, column=4) |
054 | self.db["command"] = self.dm |
055 | self.cb = Button(self) |
056 | self.cb["text"] = "Clear" |
057 | self.cb.grid(row=2, column=5) |
058 | self.cb["command"] = self.cm |
059 | self.cb2 = Button(self) |
060 | self.cb2["text"] = "Copy" |
061 | self.cb2.grid(row=2, column=6) |
062 | self.cb2["command"] = self.cm2 |
063 | |
064 | self.dt = Label(self) |
065 | m = "something happened" |
066 | self.dt["text"] = m |
067 | self.dt.grid(row=3, column=0, |
068 | columnspan=7) |
069 | |
070 | # 按下 New 按鈕的事件 |
071 | def nm(self): |
072 | self.e = Encrypt() |
073 | self.dt["text"] = self.e |
074 | |
075 | # 按下 Load 按鈕的事件 |
076 | def lm(self): |
077 | # 先測試檔案是否存在 |
078 | if os.path.exists("./code.txt"): |
079 | # 檔案存在就進行載入工作 |
080 | f = open('./code.txt', 'r') |
081 | code = f.readline() |
082 | self.e = Encrypt(code) |
083 | s = str("".join(self.e.code)) |
084 | m = "code: " + s |
085 | self.dt["text"] = m |
086 | else: |
087 | m = "Load denied!!" |
088 | self.dt["text"] = m |
089 | |
090 | # 按下 Save 按鈕的事件 |
091 | def sm(self): |
092 | # 先測試是否有按過 New 按鈕 |
093 | if self.e == None: |
094 | m = "It can not save!!" |
095 | self.dt["text"] = m |
096 | else: |
097 | # 有按過 New 按鈕就進行存檔工作 |
098 | f = open('./code.txt', 'w') |
099 | f.write("".join(self.e.code)) |
100 | f.closed |
101 | self.dt["text"] = "It's Done." |
102 | |
103 | # 按下 Encode 按鈕的事件 |
104 | def em(self): |
105 | # 取得使用者輸入 |
106 | self.userinput = self.ifd.get() |
107 | |
108 | # 先測試使用者是否有輸入 |
109 | if self.userinput == "": |
110 | m = "No input string!!" |
111 | self.dt["text"] = m |
112 | else: |
113 | # 再測試是否有按過 New 按鈕 |
114 | if self.e == None: |
115 | m = "No encrypt object!!" |
116 | self.dt["text"] = m |
117 | else: |
118 | # 使用者有輸入 |
119 | # 並且有按過 New 按鈕 |
120 | s = self.userinput |
121 | r = self.e.toEncode(s) |
122 | self.result = r |
123 | self.ofd.delete(0, 200) |
124 | self.ofd.insert(0, r) |
125 | m = "Encoding success!!" |
126 | self.dt["text"] = m |
127 | |
128 | # 按下 Decode 按鈕的事件 |
129 | def dm(self): |
130 | # 取得使用者輸入 |
131 | self.userinput = self.ifd.get() |
132 | |
133 | # 先測試使用者是否有輸入 |
134 | if self.userinput == "": |
135 | m = "No input string!!" |
136 | self.dt["text"] = m |
137 | else: |
138 | # 再測試是否有按過 New 按鈕 |
139 | if self.e == None: |
140 | m = "No encrypt object!!" |
141 | self.dt["text"] = m |
142 | else: |
143 | # 使用者有輸入 |
144 | # 並且有按過 New 按鈕 |
145 | s = self.userinput |
146 | r = self.e.toDecode(s) |
147 | self.result = r |
148 | self.ofd.delete(0, 200) |
149 | self.ofd.insert(0, r) |
150 | m = "Decoding success!!" |
151 | self.dt["text"] = m |
152 | |
153 | # 按下 Clear 按鈕的事件 |
154 | def cm(self): |
155 | self.e = None |
156 | self.userinput = "" |
157 | self.result = "" |
158 | self.ifd.delete(0, 200) |
159 | self.ofd.delete(0, 200) |
160 | |
161 | self.dt["text"] = "Clear all!!" |
162 | |
163 | # 按下 Copy 按鈕的事件 |
164 | def cm2(self): |
165 | if self.result == "": |
166 | m = "Copy denied!!" |
167 | self.dt["text"] = m |
168 | else: |
169 | self.clipboard_clear() |
170 | r = self.result |
171 | self.clipboard_append(r) |
172 | m = "It is copied." |
173 | self.dt["text"] = m |
174 | |
175 | # GUI 執行部分 |
176 | if __name__ == '__main__': |
177 | root = Tk() |
178 | app = EncryptGUI(master=root) |
179 | app.mainloop() |
180 | |
181 | # 檔名: encrypt_gui.py |
182 | # 作者: Kaiching Chang |
183 | # 時間: July, 2014 |
the end
沒有留言:
張貼留言