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
| #!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
#
import sys
running_python3 = sys.version_info[0] > 2
if running_python3:
import tkinter
import tkinter.filedialog as tkfiledialog
else:
import Tkinter as tkinter
import tkFileDialog as tkfiledialog
class Demo(tkinter.Tk):
def __init__(self):
tkinter.Tk.__init__(self)
pkvalues = {'padx': 5, 'pady': 5, 'ipadx': 2,
'ipady': 2, 'fill': 'both'}
self.e1 = tkinter.Entry(self)
self.e2 = tkinter.Entry(self)
self.e3 = tkinter.Entry(self)
self.e4 = tkinter.Entry(self)
self.e1.pack(pkvalues)
self.e2.pack(pkvalues)
self.e3.pack(pkvalues)
self.e4.pack(pkvalues)
tkinter.Button(self, text='Read',
command=self.readconfig).pack(pkvalues)
tkinter.Button(self, text='Write',
command=self.writeconfig).pack(pkvalues)
self.file_opt = options = {}
options['defaultextension'] = ''
options['filetypes'] = [('all files', '.*'), ('config files', '.cfg')]
options['initialfile'] = 'conf.cfg'
options['parent'] = self
options['title'] = 'Configuration'
self.widgets = (self.e1, self.e2, self.e3, self.e4)
def readconfig(self):
conffile = tkfiledialog.askopenfile(mode='r', **self.file_opt)
if conffile:
for l in conffile.readlines():
v = l.split()
t = v[1]
w = self.widgets[int(v[0])]
if t == 'None':
t = ''
w.delete(0, tkinter.END)
w.insert(0, t)
def writeconfig(self):
conffile = tkfiledialog.asksaveasfilename(**self.file_opt)
if conffile:
with open(conffile, 'w') as f:
for i, w in enumerate(self.widgets):
val = w.get()
if val == '':
val = 'None'
f.write("%d %s\n" % (i, val))
if __name__ == "__main__":
root = Demo()
root.mainloop() |
Partager