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



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
from tkinter import *
from tkinter.ttk import *
  
# Encrypt 的 GUI 類別
class EncryptGUI(Frame):
   # 設定初值
   def __init__(self, master=None):
      Frame.__init__(self, master)
      self.grid()
      self.createWidgets()
 
   # 建立所有視窗元件
   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.dt["text"] = "New Button."
 
   # 按下 Load 按鈕的事件
   def lm(self):
      self.dt["text"] = "Load Button."
 
   # 按下 Save 按鈕的事件
   def sm(self):
      self.dt["text"] = "Save Button."
 
   # 按下 Encode 按鈕的事件
   def em(self):
      self.dt["text"] = "Encode Button."
 
   # 按下 Decode 按鈕的事件
   def dm(self):
      self.dt["text"] = "Decode Button."
 
   # 按下 Clear 按鈕的事件
   def cm(self):
      self.dt["text"] = "Clear Button."
 
   # 按下 Copy 按鈕的事件
   def cm2(self):
      self.dt["text"] = "Copy Button."
 
# GUI 執行部分
if __name__ == '__main__':
   root = Tk()
   app = EncryptGUI(master=root)
   app.mainloop()
  
# 檔名: encrypt_gui.py
# 作者: Kaiching Chang
# 時間: July, 2014

tk_demo2.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
from tkinter import *
from tkinter.ttk import *
 
class TkDemo(Frame):
   def __init__(self, master=None):
      Frame.__init__(self, master)
      self.grid()
      self.createWidgets()
      self.number = 0
 
   def createWidgets(self):
      self.clickme = Button(self)
      self.clickme["text"] = "Click Me!"
      self.clickme.grid(row=0, column=0)
      self.clickme["command"] = self.clickMethod
      self.display = Label(self)
      self.display["text"] = "Start!"
      self.display.grid(row=1, column=0)
 
   def clickMethod(self):
      self.number += 1
      if self.number % 2 == 0:
         self.display["text"] = "What?"
      else:
         self.display["text"] = "Click!"
 
if __name__ == '__main__':
   root = Tk()
   app = TkDemo(master=root)
   app.mainloop()
  
# 檔名: tk_demo2.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

沒有留言: