I can not get it to run. S
sorry about that. baybe it is a conflict between tabstop width in my editor and in my newsreader.
i substituted tabs with spaces and i hope now you can run it.
bye
macs
----cuthere----
from Tkinter import *
class MiaApp: def __init__(self, genitore):
self.MioGenitore = genitore
fonte = ("Helvetica", "12")
self.campi = ["Cognome", "Nome" , "Data di nascita"]
quadro_grande = Frame(genitore) quadro_grande.pack(expand = YES, fill = BOTH)
quadro_menu = Frame(quadro_grande) quadro_menu.configure( bd = 1, relief = RAISED ) quadro_menu.pack(side = TOP, fill = X)
pm_file = Menubutton(quadro_menu) pm_file.configure(text = "File") pm_file.pack(side = LEFT)
m_file = Menu(pm_file) pm_file.configure(menu = m_file) m_file.configure(tearoff = NO) m_file.add_command( label = "Scrivi", command = self.premuto_scrivi ) m_file.add_command( label = "Leggi", command = self.premuto_leggi ) m_file.add_separator() m_file.add_command( label = "Chiudi", command = genitore.destroy )
pm_mod = Menubutton(quadro_menu) pm_mod.configure(text = "Modifica") pm_mod.pack(side = LEFT)
pm_aiuto = Menubutton(quadro_menu) pm_aiuto.configure(text = "?") pm_aiuto.pack(side = RIGHT)
msg = Label(quadro_grande) msg.configure( font = fonte, relief = RIDGE, wraplength = "10c", justify = LEFT, text = u"Questo \u00E8 un programma in Python \ che trae ispirazione da un analogo lavoro del collega \ G. Renda. Il programma originale era scritto \ in linguaggio Java, e sfruttava le librerie JFC \ (\u00ABJava Foundation Class\u00BB, dette anche \ \u00ABSwing\u00BB); questo invece usa le librerie Tk, \ mediante il modulo Tkinter." ) msg.pack( side = TOP, padx = "2m", pady = "2m" )
quadro_pulsanti = Frame(quadro_grande) quadro_pulsanti.pack( side = BOTTOM, fill = X, padx = "2m", pady = "2m" )
scrivi = Button(quadro_pulsanti) scrivi.configure( text = "Scrivi", command = self.premuto_scrivi ) scrivi.pack(side = LEFT, expand = YES)
leggi = Button(quadro_pulsanti) leggi.configure(text = "Leggi", command = self.premuto_leggi) leggi.pack(side = LEFT, expand = YES)
chiudi = Button(quadro_pulsanti) chiudi.configure(text = "Chiudi", command = genitore.destroy) chiudi.pack(side = LEFT, expand = YES)
def premuto_scrivi(self): InserimentoRecord()
def premuto_leggi(self): ConsultazioneRecord()
class InserimentoRecord(Toplevel): def __init__(self):
Toplevel.__init__(self)
self.titolo = "Inserimento" self.wm_title(self.titolo)
quadro_grande = Frame(self) quadro_grande.pack(expand = YES, fill = BOTH)
self.quadro_pulsanti = Frame(quadro_grande) self.quadro_pulsanti.pack( side = BOTTOM, fill = X, padx = "2m", pady = "2m" )
quadri_ing = [] self.n = len(miaApp.campi) self.var = [] eti = [] larg_eti = max(map(len, miaApp.campi)) ing = [] for i in range(self.n): quadri_ing.append(None) self.var.append(None) ing.append(None) eti.append(None) quadri_ing[i] = Frame(quadro_grande) quadri_ing[i].pack(side = TOP, expand = YES, fill = BOTH) self.var[i] = StringVar() eti[i] = Label(quadri_ing[i]) eti[i].configure( text = miaApp.campi[i] + ": ", width = larg_eti, anchor = E ) eti[i].pack(side = LEFT, pady = 5, padx = 10, fill = X) ing[i] = Entry(quadri_ing[i], textvariable = self.var[i]) ing[i].pack(side = LEFT, pady = 5, padx = 10, fill = X)
self.salva = Button(self.quadro_pulsanti) self.salva.configure( text = "Salva", command = self.premuto_salva ) self.salva.pack(side = LEFT, expand = YES)
self.annulla = Button(self.quadro_pulsanti) self.annulla.configure( text = "Annulla", command = self.premuto_annulla ) self.annulla.pack(side = LEFT, expand = YES)
def premuto_salva(self): import tkFileDialog import pickle
dati = [] for i in range(self.n): dati.append(None) dati[i] = self.var[i].get()
nomefile = tkFileDialog.asksaveasfilename( defaultextension = ".ana", filetypes = [ ("Record anagrafici", "*.ana"), ("Tutti i file", "*") ] )
if nomefile: f = open(nomefile, "w") pickle.dump(dati, f) f.close()
self.destroy()
def premuto_annulla(self): self.destroy()
class ConsultazioneRecord(Toplevel): def __init__(self):
import tkFileDialog
nomefile = tkFileDialog.askopenfilename( defaultextension = ".ana", filetypes = [ ("Record anagrafici", "*.ana"), ("Tutti i file", "*") ] )
if nomefile:
try: ### Il metodo 'pickle.load' potrebbe generare una ### eccezione se l'utente cerca di aprire un file ### non del formato giusto
Toplevel.__init__(self)
self.titolo = "Consultazione" self.wm_title(self.titolo)
quadro_grande = Frame(self) quadro_grande.pack(expand = YES, fill = BOTH)
eti_rec = Label(quadro_grande) eti_rec.configure( text = "Record: " + nomefile, relief = RIDGE ) eti_rec.pack( side = TOP, fill = X, padx = "5m", pady = "5m" )
self.quadro_pulsanti = Frame(quadro_grande) self.quadro_pulsanti.pack( side = BOTTOM, fill = X, padx = "2m", pady = "2m" )
import pickle
quadri_eti = [] eti_campi = [] eti_val = [] larg_eti_campi = max(map(len, miaApp.campi)) f = open(nomefile, "r")
valori = pickle.load(f) ### Potrebbe generare una ### eccezione 'KeyError'
larg_eti_val = max(map(len, valori)) n = len(miaApp.campi) for i in range(n): quadri_eti.append(None) eti_campi.append(None) eti_val.append(None) quadri_eti[i] = Frame(quadro_grande) quadri_eti[i].pack(side = TOP, expand = YES, fill = BOTH) eti_campi[i] = Label( quadri_eti[i], text = miaApp.campi[i] + ": ", width = larg_eti_campi, anchor = E ) eti_campi[i].pack(side = LEFT, pady = 5, padx = 10, fill = X) eti_val[i] = Label( quadri_eti[i], text = valori[i], anchor = W, width = larg_eti_val, relief = SUNKEN ) eti_val[i].pack(side = LEFT, pady = 5, padx = 10, fill = X) self.chiudi = Button(self.quadro_pulsanti) self.chiudi.configure( text = "Chiudi", command = self.premuto_chiudi ) self.chiudi.pack(side = LEFT, expand = YES)
except KeyError: ### Eventualmente generato da ### 'pickle.load()'
import tkMessageBox
tkMessageBox.showwarning( "Bad input", "Illegal values, please try again" ) self.destroy()
f.close()
def premuto_chiudi(self): self.destroy()
radice = Tk() radice.wm_title("Versione Python di un programma Java di G. Renda") radice.wm_iconname("giuseppe") radice.minsize(400, 300) miaApp = MiaApp(radice) radice.mainloop() -- http://mail.python.org/mailman/listinfo/python-list