python学习(五)

Tkinter的学习

1.编写简单的计算器

 1 #coding:utf-8
 2 
 3 from Tkinter import *
 4 
 5 class App:
 6     def __init__(self, root):
 7         frame = Frame(root)
 8         frame.pack()
 9         self.frame = frame
10 
11         lab = Label(frame, text = u"简单计算器")
12         lab.pack()
13         
14         self.new_input()
15         
16         //注意传参的方法
17         btn1 = Button(frame, text = "1", fg = "green", command = lambda : self.buttoncb(1))
18         btn2 = Button(frame, text = "2", fg = "green", command = lambda : self.buttoncb(2))
19         btn3 = Button(frame, text = "3", fg = "green", command = lambda : self.buttoncb(3))
20         btn4 = Button(frame, text = "4", fg = "green", command = lambda : self.buttoncb(4))
21         btn5 = Button(frame, text = "5", fg = "green", command = lambda : self.buttoncb(5))
22         btn6 = Button(frame, text = "6", fg = "green", command = lambda : self.buttoncb(6))
23         btn7 = Button(frame, text = "7", fg = "green", command = lambda : self.buttoncb(7))
24         btn8 = Button(frame, text = "8", fg = "green", command = lambda : self.buttoncb(8))
25         btn9 = Button(frame, text = "9", fg = "green", command = lambda : self.buttoncb(9))
26         btn0 = Button(frame, text = "0", fg = "green", command = lambda : self.buttoncb(0))
27         btnadd = Button(frame, text = "+", fg = "green", command = lambda:self.buttoncb('+'))
28         btnsub = Button(frame, text = "-", fg = "green", command = lambda:self.buttoncb('-'))
29         btnchen = Button(frame, text = "*", fg = "green", command = lambda:self.buttoncb('*'))
30         btndiv = Button(frame, text = "%", fg = "green", command = lambda:self.buttoncb('%'))
31         btnque = Button(frame, text = "=", fg = "green", command = self.cal)
32 
33         btn1.pack()
34         btn2.pack()
35         btn3.pack()
36         btn4.pack()        
37         btn5.pack()
38         btn6.pack()
39         btn7.pack()
40         btn8.pack()
41         btn9.pack()
42         btn0.pack()
43         btnadd.pack()
44         btnsub.pack()
45         btnchen.pack()
46         btndiv.pack()
47         btnque.pack()
48 
49         button = Button(frame, text = "Quit",fg = "blue", command = root.quit)
50         button.pack(side = BOTTOM)
51     
52     def new_input(self):
53         v = StringVar()
54         e = Entry(self.frame, textvariable = v)
55         #v.set("")
56         self.v = v
57         e.pack()
58 
59     def buttoncb(self, i):
60         #print "button."
61         val = self.v.get()
62         self.v.set(val + str(i))
63         
64         
65     def cal(self):
66         val = self.v.get()
67         val1 = eval(val)
68         self.v.set(val1)
69 
70 root = Tk()
71 a = App(root)
72 #a.new_input()
73 root.mainloop()

2.编写计时器

 1 #coding:utf-8
 2 import Tkinter as tk
 3 import time
 4 
 5 class App():
 6     def __init__(self):
 7         self.root = tk.Tk()
 8         self.label = tk.Label(text="计时器", fg = "red")
 9         self.label.pack()
10         self.update_clock()
11         self.root.mainloop()
12 
13     def update_clock(self):
14         now = time.strftime("%H:%M:%S")
15         self.label.configure(text = now)
16         self.root.after(1000, self.update_clock)
17 
18 app = App()
原文地址:https://www.cnblogs.com/goodhacker/p/3163691.html