Python 入門指南 V2.00 - 單元 25 範例及練習程式碼



encrypt_gui.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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


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
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

沒有留言: