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



tk_demo.py


1
2
3
4
5
6
7
8
9
10
11
12
from tkinter import *
   
root = Tk()
text = Label(root, text="Tk's job!!",
             width="30", height="5",
             bg="black", fg="white")
text.pack()
root.mainloop()
  
# 檔名: tk_demo.py
# 作者: Kaiching Chang
# 時間: July, 2014

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
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.lb = Button(self)
      self.lb["text"] = "Load"
      self.lb.grid(row=2, column=1)
      self.sb = Button(self)
      self.sb["text"] = "Save"
      self.sb.grid(row=2, column=2)
      self.eb = Button(self)
      self.eb["text"] = "Encode"
      self.eb.grid(row=2, column=3)
      self.db = Button(self)
      self.db["text"] = "Decode"
      self.db.grid(row=2, column=4)
      self.cb = Button(self)
      self.cb["text"] = "Clear"
      self.cb.grid(row=2, column=5)
      self.cb2 = Button(self)
      self.cb2["text"] = "Copy"
      self.cb2.grid(row=2, column=6)
  
      self.dt = Label(self)
      m = "something happened"
      self.dt["text"] = m
      self.dt.grid(row=3, column=0, columnspan=7)
 
# 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
from tkinter import *
from tkinter.ttk import *
 
class Demo2(Frame):
   # 設定初值
   def __init__(self, master=None):
      Frame.__init__(self, master)
      self.grid()
      self.createWidgets()
 
   # 建立所有視窗元件
   def createWidgets(self):
      self.display = Label(self)
      self.display["text"] = "Hello~~"
      self.display.grid(row=0, column=0)
      self.click = Button(self)
      self.click["text"] = "Click Me"
      self.click.grid(row=1, column=0)
 
# GUI 執行部分
if __name__ == '__main__':
   root = Tk()
   app = Demo2(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
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()
  
# 檔名: guessgame_gui.py
# 作者: Kaiching Chang
# 時間: July, 2014

the end

2 則留言:

Earl 提到...

第一個練習範例, 檔名&註解 好像放錯了

Kaiching Chang 提到...

這邊放錯了,已修改,感謝指正 ^_^