周末在家学习了python的Tkinter,感觉这东西简单好用。
# !/usr/bin/env python # coding=utf-8 from Tkinter import * import ttk import tkMessageBox filename = 'test.txt' def defocus(event): event.widget.master.focus_set() def DisplayHello(event): # win = Toplevel() # Label(win,text='Hello, Tkinter').pack(pady=15) # tkMessageBox.showinfo('Response','Thanks for clicking the button.') tkMessageBox.askquestion('MSG', '要保存吗?',savefile) root =Tk(className="main") def savefile(event): textfile = open(filename, 'a') #for how to solve chinese coding, see #http://flyantme.blog.163.com/blog/static/7586977520096711026111/ first = txtfirst.get().encode('utf-8') last = txtlast.get().encode('utf-8') address = txtadd.get().encode('utf-8') city = txtcity.get().encode('utf-8') state = txtst.get().encode('utf-8') zipcode = txtzip.get().encode('utf-8') phone = txtph.get().encode('utf-8') email = txtemail.get().encode('utf-8') step = comboBox1.get().encode('utf-8') print >> textfile, last, '\n', first, '\n', address, '\n', city, '\n', state, '\n', zipcode, '\n', phone, '\n', step, '\n',email textfile.close() # root.destroy() root.geometry('500x300+400+400') root.maxsize(500, 300) root.minsize(500, 300) d = Frame(root) fname = Label(d, width = 10,text = u'ip') fname.pack(side = 'left',fill ='x', pady = 1) txtfirst = Entry(d, width =40) txtfirst.pack(side = 'left') d.pack(side = 'top',fill ='x',pady = 1) #d.place(x =0, y =0, anchor = NW) e = Frame(root) lname = Label(e, width = 10,text = u'姓') lname.pack(side = 'left',pady = 1) txtlast = Entry(e, width =40) txtlast.pack(side = 'left') e.pack(side = 'top',fill ='x',pady = 1) f = Frame(root) address = Label(f, width = 10,text = u'街道') address.pack(side = 'left',pady = 1) txtadd = Entry(f, width =40) txtadd.pack(side = 'left') f.pack(side = 'top',fill ='x') g = Frame(root) city = Label(g, width = 10,text = u'市') city.pack(side = 'left',pady = 1) txtcity = Entry(g, width =16) txtcity.pack(side = 'left') street = Label(g, width = 10,text = u'省') street.pack(side = 'left',pady = 1) txtst = Entry(g, width =4) txtst.pack(side = 'left') zipcde = Label(g, width = 10,text = u'邮政编码') zipcde.pack(side = 'left',pady = 1) txtzip = Entry(g, width =8) txtzip.pack(side = 'left') g.pack(side = 'top',fill ='x') h = Frame(root) phone = Label(h, width = 10,text = u'电话') phone.pack(side = 'left',pady = 1) txtph = Entry(h, width =16) txtph.pack(side = 'left') email = Label(h, width = 10,text = 'E-mail') email.pack(side = 'left',pady = 1) txtemail = Entry(h, width =16) txtemail.pack(side = 'left') h.pack(side = 'top',fill ='x') x = Frame(root) phone = Label(x, width = 10,text = u'Step1') phone.pack(side = 'left',pady = 1) comboBox1 = ttk.Combobox(x, state="readonly", values=("a", "b", "c",""),width = 26) comboBox1.pack(side = 'left') comboBox1.set("") comboBox1.bind("<FocusIn>", defocus) x.pack(side = 'top',fill ='x',pady = 1) y = Frame(root) phone = Label(y, width = 10,text = u'Step2') phone.pack(side = 'left',pady = 1) comboBox = ttk.Combobox(y, state="readonly", values=("a", "b", "c",""),width = 26) comboBox.pack(side = 'left') comboBox.set("") comboBox.bind("<FocusIn>", defocus) y.pack(side = 'top',fill ='x',pady = 1) j=Frame(root) save = Button(j, text = u"保存", width = 10) save.pack(pady=1, padx =30) save.bind("<Button-1>", savefile) #save.bind("<Button-1>", DisplayHello) j.pack(side='left',fill='y') i = Frame(root) close = Button(i, text = u"取消",command=root.destroy) close.pack(side = 'left',pady = 1, padx = 50) i.pack(side = 'top',fill ='x') root.mainloop()
出来的效果: