encrypt_gui.py
from tkinter import * from tkinter.ttk import * from encrypt import Encrypt import os # Encrypt 的 GUI 類別 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.it = Label(self) self.it["text"] = "Input:" self.it.grid(row=0, column=0) self.ifd = Entry(self) self.ifd["width"] = 60 self.ifd.grid(row=0, column=1, columnspan=6) self.ot = Label(self) self.ot["text"] = "Output:" self.ot.grid(row=1, column=0) self.ofd = Entry(self) self.ofd["width"] = 60 self.ofd.grid(row=1, column=1, columnspan=6) self.nb = Button(self) self.nb["text"] = "New" self.nb.grid(row=2, column=0) self.nb["command"] = self.nm self.lb = Button(self) self.lb["text"] = "Load" self.lb.grid(row=2, column=1) self.lb["command"] = self.lm self.sb = Button(self) self.sb["text"] = "Save" self.sb.grid(row=2, column=2) self.sb["command"] = self.sm self.eb = Button(self) self.eb["text"] = "Encode" self.eb.grid(row=2, column=3) self.eb["command"] = self.em self.db = Button(self) self.db["text"] = "Decode" self.db.grid(row=2, column=4) self.db["command"] = self.dm self.cb = Button(self) self.cb["text"] = "Clear" self.cb.grid(row=2, column=5) self.cb["command"] = self.cm self.cb2 = Button(self) self.cb2["text"] = "Copy" self.cb2.grid(row=2, column=6) self.cb2["command"] = self.cm2 self.dt = Label(self) m = "something happened" self.dt["text"] = m self.dt.grid(row=3, column=0, columnspan=7) # 按下 New 按鈕的事件 def nm(self): self.e = Encrypt() self.dt["text"] = self.e # 按下 Load 按鈕的事件 def lm(self): # 先測試檔案是否存在 if os.path.exists("./code.txt"): # 檔案存在就進行載入工作 f = open('./code.txt', 'r') code = f.readline() self.e = Encrypt(code) s = str("".join(self.e.code)) m = "code: " + s self.dt["text"] = m else: m = "Load denied!!" self.dt["text"] = m # 按下 Save 按鈕的事件 def sm(self): # 先測試是否有按過 New 按鈕 if self.e == None: m = "It can not save!!" self.dt["text"] = m else: # 有按過 New 按鈕就進行存檔工作 f = open('./code.txt', 'w') f.write("".join(self.e.code)) f.closed self.dt["text"] = "It's Done." # 按下 Encode 按鈕的事件 def em(self): # 取得使用者輸入 self.userinput = self.ifd.get() # 先測試使用者是否有輸入 if self.userinput == "": m = "No input string!!" self.dt["text"] = m else: # 再測試是否有按過 New 按鈕 if self.e == None: m = "No encrypt object!!" self.dt["text"] = m else: # 使用者有輸入 # 並且有按過 New 按鈕 s = self.userinput r = self.e.toEncode(s) self.result = r self.ofd.delete(0, 200) self.ofd.insert(0, r) m = "Encoding success!!" self.dt["text"] = m # 按下 Decode 按鈕的事件 def dm(self): # 取得使用者輸入 self.userinput = self.ifd.get() # 先測試使用者是否有輸入 if self.userinput == "": m = "No input string!!" self.dt["text"] = m else: # 再測試是否有按過 New 按鈕 if self.e == None: m = "No encrypt object!!" self.dt["text"] = m else: # 使用者有輸入 # 並且有按過 New 按鈕 s = self.userinput r = self.e.toDecode(s) self.result = r self.ofd.delete(0, 200) self.ofd.insert(0, r) m = "Decoding success!!" self.dt["text"] = m # 按下 Clear 按鈕的事件 def cm(self): self.e = None self.userinput = "" self.result = "" self.ifd.delete(0, 200) self.ofd.delete(0, 200) self.dt["text"] = "Clear all!!" # 按下 Copy 按鈕的事件 def cm2(self): if self.result == "": m = "Copy denied!!" self.dt["text"] = m else: self.clipboard_clear() r = self.result self.clipboard_append(r) m = "It is copied." self.dt["text"] = m # GUI 執行部分 if __name__ == '__main__': root = Tk() app = EncryptGUI(master=root) app.mainloop() # 檔名: encrypt_gui.py # 作者: Kaiching Chang # 時間: July, 2014
guessgame_gui.py
from tkinter import * from tkinter.ttk import * from guessgame import * class GuessGameGUI(Frame): # 設定初值 def __init__(self, master=None): Frame.__init__(self, master) self.grid() self.createWidgets() self.game = None self.guess = "" # 建立所有視窗元件 def createWidgets(self): self.input = Label(self) self.input["text"] = "number:" self.input.grid(row=0, column=0) self.inputentry = Entry(self) self.inputentry["width"] = 15 self.inputentry.grid(row=0, column=1, columnspan=3) self.newgame = Button(self) self.newgame["text"] = "New" self.newgame.grid(row=1, column=0, columnspan=2) self.newgame["command"] = self.newMethod self.guessbutton = Button(self) self.guessbutton["text"] = "Guess" self.guessbutton.grid(row=1, column=2, columnspan=2) self.guessbutton["command"] = self.guessMethod self.display = Label(self) self.display["text"] = "something happened" self.display.grid(row=2, column=0, columnspan=4) def newMethod(self): self.game = GuessGame() self.display["text"] = "New game." def guessMethod(self): if self.game == None: self.display["text"] = "Press New first!" else: self.game.times += 1 self.guess = self.inputentry.get() if len(self.guess) != 4: self.display["text"] = "Wrong length!!" elif self.game.find_number(self.guess): self.display["text"] = "Repeating digits!!" else: self.game.ab_counter(self.guess) if self.game.a == 4: self.display["text"] = "Congratulation!! " + str(self.game.times) + " times!!" else: self.display["text"] = str(self.game.a) + "A" + str(self.game.b) + "B" # GUI 執行部分 if __name__ == '__main__': root = Tk() app = GuessGameGUI(master=root) app.mainloop() # 檔名: guessgame_gui.py # 作者: Kaiching Chang # 時間: July, 2014
the end
沒有留言:
張貼留言