hello.
i wrote a very simple tkinter demo program that uses menus, buttons,
labels, entries, frames and secondary toplevels.
it is a python version of a java program made by a colleague.
the user can create ("Scrivi") a record with his second name, first name
and date of birth, save ("Salva") the record to a file or read in
("Leggi") a previously created file. "Annulla" is to cancel. "Chiudi" is
to close.
i also used try-except for checking for invalid files.
only one menu is real ("File"), the others are for future use.
i'd like to hear your suggestions and comments for improving it.
i recently learned tkiner from stephen ferg's website and reading the
tk8.4 widget demo programs.
one thing i still cannot do is to make the primary window and the
secondary aware of each other (so avoid that a second instance of a
secondary window can be generated if an instance still exists, for example).
so, here it is folks:
---cut here---
from Tkinter import *
class MiaApp:
def __init__(self, genitore):
self.MioGenitor e = genitore
fonte = ("Helvetica" , "12")
self.campi = ["Cognome", "Nome" , "Data di nascita"]
quadro_grande = Frame(genitore)
quadro_grande.p ack(expand = YES, fill = BOTH)
quadro_menu = Frame(quadro_gr ande)
quadro_menu.con figure(
bd = 1,
relief = RAISED
)
quadro_menu.pac k(side = TOP, fill = X)
pm_file = Menubutton(quad ro_menu)
pm_file.configu re(text = "File")
pm_file.pack(si de = LEFT)
m_file = Menu(pm_file)
pm_file.configu re(menu = m_file)
m_file.configur e(tearoff = NO)
m_file.add_comm and(
label = "Scrivi",
command = self.premuto_sc rivi
)
m_file.add_comm and(
label = "Leggi",
command = self.premuto_le ggi
)
m_file.add_sepa rator()
m_file.add_comm and(
label = "Chiudi",
command = genitore.destro y
)
pm_mod = Menubutton(quad ro_menu)
pm_mod.configur e(text = "Modifica")
pm_mod.pack(sid e = LEFT)
pm_aiuto = Menubutton(quad ro_menu)
pm_aiuto.config ure(text = "?")
pm_aiuto.pack(s ide = RIGHT)
msg = Label(quadro_gr ande)
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\u00 BB); questo invece usa le librerie Tk, \
mediante il modulo Tkinter."
)
msg.pack(
side = TOP,
padx = "2m",
pady = "2m"
)
quadro_pulsanti = Frame(quadro_gr ande)
quadro_pulsanti .pack(
side = BOTTOM,
fill = X,
padx = "2m",
pady = "2m"
)
scrivi = Button(quadro_p ulsanti)
scrivi.configur e(
text = "Scrivi",
command = self.premuto_sc rivi
)
scrivi.pack(sid e = LEFT, expand = YES)
leggi = Button(quadro_p ulsanti)
leggi.configure (text = "Leggi", command = self.premuto_le ggi)
leggi.pack(side = LEFT, expand = YES)
chiudi = Button(quadro_p ulsanti)
chiudi.configur e(text = "Chiudi", command = genitore.destro y)
chiudi.pack(sid e = LEFT, expand = YES)
def premuto_scrivi( self):
InserimentoReco rd()
def premuto_leggi(s elf):
ConsultazioneRe cord()
class InserimentoReco rd(Toplevel):
def __init__(self):
Toplevel.__init __(self)
self.titolo = "Inseriment o"
self.wm_title(s elf.titolo)
quadro_grande = Frame(self)
quadro_grande.p ack(expand = YES, fill = BOTH)
self.quadro_pul santi = Frame(quadro_gr ande)
self.quadro_pul santi.pack(
side = BOTTOM,
fill = X,
padx = "2m",
pady = "2m"
)
quadri_ing = []
self.n = len(miaApp.camp i)
self.var = []
eti = []
larg_eti = max(map(len, miaApp.campi))
ing = []
for i in range(self.n):
quadri_ing.appe nd(None)
self.var.append (None)
ing.append(None )
eti.append(None )
quadri_ing[i] = Frame(quadro_gr ande)
quadri_ing[i].pack(side = TOP, expand = YES, fill = BOTH)
self.var[i] = StringVar()
eti[i] = Label(quadri_in g[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_in g[i], textvariable = self.var[i])
ing[i].pack(side = LEFT, pady = 5, padx = 10, fill = X)
self.salva = Button(self.qua dro_pulsanti)
self.salva.conf igure(
text = "Salva",
command = self.premuto_sa lva
)
self.salva.pack (side = LEFT, expand = YES)
self.annulla = Button(self.qua dro_pulsanti)
self.annulla.co nfigure(
text = "Annulla",
command = self.premuto_an nulla
)
self.annulla.pa ck(side = LEFT, expand = YES)
def premuto_salva(s elf):
import tkFileDialog
import pickle
dati = []
for i in range(self.n):
dati.append(Non e)
dati[i] = self.var[i].get()
nomefile = tkFileDialog.as ksaveasfilename (
defaultextensio n = ".ana",
filetypes = [
("Record anagrafici", "*.ana"),
("Tutti i file", "*")
]
)
if nomefile:
f = open(nomefile, "w")
pickle.dump(dat i, f)
f.close()
self.destroy()
def premuto_annulla (self):
self.destroy()
class ConsultazioneRe cord(Toplevel):
def __init__(self):
import tkFileDialog
nomefile = tkFileDialog.as kopenfilename(
defaultextensio n = ".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 = "Consultazi one"
self.wm_title(s elf.titolo)
quadro_grande = Frame(self)
quadro_grande.p ack(expand = YES, fill = BOTH)
eti_rec = Label(quadro_gr ande)
eti_rec.configu re(
text = "Record: " + nomefile,
relief = RIDGE
)
eti_rec.pack(
side = TOP,
fill = X,
padx = "5m",
pady = "5m"
)
self.quadro_pul santi = Frame(quadro_gr ande)
self.quadro_pul santi.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.camp i)
for i in range(n):
quadri_eti.appe nd(None)
eti_campi.appen d(None)
eti_val.append( None)
quadri_eti[i] = Frame(quadro_gr ande)
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.qua dro_pulsanti)
self.chiudi.con figure(
text = "Chiudi",
command = self.premuto_ch iudi
)
self.chiudi.pac k(side = LEFT, expand = YES)
except KeyError: ### Eventualmente generato da
### 'pickle.load()'
import tkMessageBox
tkMessageBox.sh owwarning(
"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_iconn ame("giuseppe")
radice.minsize( 400, 300)
miaApp = MiaApp(radice)
radice.mainloop ()
i wrote a very simple tkinter demo program that uses menus, buttons,
labels, entries, frames and secondary toplevels.
it is a python version of a java program made by a colleague.
the user can create ("Scrivi") a record with his second name, first name
and date of birth, save ("Salva") the record to a file or read in
("Leggi") a previously created file. "Annulla" is to cancel. "Chiudi" is
to close.
i also used try-except for checking for invalid files.
only one menu is real ("File"), the others are for future use.
i'd like to hear your suggestions and comments for improving it.
i recently learned tkiner from stephen ferg's website and reading the
tk8.4 widget demo programs.
one thing i still cannot do is to make the primary window and the
secondary aware of each other (so avoid that a second instance of a
secondary window can be generated if an instance still exists, for example).
so, here it is folks:
---cut here---
from Tkinter import *
class MiaApp:
def __init__(self, genitore):
self.MioGenitor e = genitore
fonte = ("Helvetica" , "12")
self.campi = ["Cognome", "Nome" , "Data di nascita"]
quadro_grande = Frame(genitore)
quadro_grande.p ack(expand = YES, fill = BOTH)
quadro_menu = Frame(quadro_gr ande)
quadro_menu.con figure(
bd = 1,
relief = RAISED
)
quadro_menu.pac k(side = TOP, fill = X)
pm_file = Menubutton(quad ro_menu)
pm_file.configu re(text = "File")
pm_file.pack(si de = LEFT)
m_file = Menu(pm_file)
pm_file.configu re(menu = m_file)
m_file.configur e(tearoff = NO)
m_file.add_comm and(
label = "Scrivi",
command = self.premuto_sc rivi
)
m_file.add_comm and(
label = "Leggi",
command = self.premuto_le ggi
)
m_file.add_sepa rator()
m_file.add_comm and(
label = "Chiudi",
command = genitore.destro y
)
pm_mod = Menubutton(quad ro_menu)
pm_mod.configur e(text = "Modifica")
pm_mod.pack(sid e = LEFT)
pm_aiuto = Menubutton(quad ro_menu)
pm_aiuto.config ure(text = "?")
pm_aiuto.pack(s ide = RIGHT)
msg = Label(quadro_gr ande)
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\u00 BB); questo invece usa le librerie Tk, \
mediante il modulo Tkinter."
)
msg.pack(
side = TOP,
padx = "2m",
pady = "2m"
)
quadro_pulsanti = Frame(quadro_gr ande)
quadro_pulsanti .pack(
side = BOTTOM,
fill = X,
padx = "2m",
pady = "2m"
)
scrivi = Button(quadro_p ulsanti)
scrivi.configur e(
text = "Scrivi",
command = self.premuto_sc rivi
)
scrivi.pack(sid e = LEFT, expand = YES)
leggi = Button(quadro_p ulsanti)
leggi.configure (text = "Leggi", command = self.premuto_le ggi)
leggi.pack(side = LEFT, expand = YES)
chiudi = Button(quadro_p ulsanti)
chiudi.configur e(text = "Chiudi", command = genitore.destro y)
chiudi.pack(sid e = LEFT, expand = YES)
def premuto_scrivi( self):
InserimentoReco rd()
def premuto_leggi(s elf):
ConsultazioneRe cord()
class InserimentoReco rd(Toplevel):
def __init__(self):
Toplevel.__init __(self)
self.titolo = "Inseriment o"
self.wm_title(s elf.titolo)
quadro_grande = Frame(self)
quadro_grande.p ack(expand = YES, fill = BOTH)
self.quadro_pul santi = Frame(quadro_gr ande)
self.quadro_pul santi.pack(
side = BOTTOM,
fill = X,
padx = "2m",
pady = "2m"
)
quadri_ing = []
self.n = len(miaApp.camp i)
self.var = []
eti = []
larg_eti = max(map(len, miaApp.campi))
ing = []
for i in range(self.n):
quadri_ing.appe nd(None)
self.var.append (None)
ing.append(None )
eti.append(None )
quadri_ing[i] = Frame(quadro_gr ande)
quadri_ing[i].pack(side = TOP, expand = YES, fill = BOTH)
self.var[i] = StringVar()
eti[i] = Label(quadri_in g[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_in g[i], textvariable = self.var[i])
ing[i].pack(side = LEFT, pady = 5, padx = 10, fill = X)
self.salva = Button(self.qua dro_pulsanti)
self.salva.conf igure(
text = "Salva",
command = self.premuto_sa lva
)
self.salva.pack (side = LEFT, expand = YES)
self.annulla = Button(self.qua dro_pulsanti)
self.annulla.co nfigure(
text = "Annulla",
command = self.premuto_an nulla
)
self.annulla.pa ck(side = LEFT, expand = YES)
def premuto_salva(s elf):
import tkFileDialog
import pickle
dati = []
for i in range(self.n):
dati.append(Non e)
dati[i] = self.var[i].get()
nomefile = tkFileDialog.as ksaveasfilename (
defaultextensio n = ".ana",
filetypes = [
("Record anagrafici", "*.ana"),
("Tutti i file", "*")
]
)
if nomefile:
f = open(nomefile, "w")
pickle.dump(dat i, f)
f.close()
self.destroy()
def premuto_annulla (self):
self.destroy()
class ConsultazioneRe cord(Toplevel):
def __init__(self):
import tkFileDialog
nomefile = tkFileDialog.as kopenfilename(
defaultextensio n = ".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 = "Consultazi one"
self.wm_title(s elf.titolo)
quadro_grande = Frame(self)
quadro_grande.p ack(expand = YES, fill = BOTH)
eti_rec = Label(quadro_gr ande)
eti_rec.configu re(
text = "Record: " + nomefile,
relief = RIDGE
)
eti_rec.pack(
side = TOP,
fill = X,
padx = "5m",
pady = "5m"
)
self.quadro_pul santi = Frame(quadro_gr ande)
self.quadro_pul santi.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.camp i)
for i in range(n):
quadri_eti.appe nd(None)
eti_campi.appen d(None)
eti_val.append( None)
quadri_eti[i] = Frame(quadro_gr ande)
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.qua dro_pulsanti)
self.chiudi.con figure(
text = "Chiudi",
command = self.premuto_ch iudi
)
self.chiudi.pac k(side = LEFT, expand = YES)
except KeyError: ### Eventualmente generato da
### 'pickle.load()'
import tkMessageBox
tkMessageBox.sh owwarning(
"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_iconn ame("giuseppe")
radice.minsize( 400, 300)
miaApp = MiaApp(radice)
radice.mainloop ()
Comment