473,382 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

very simple tkinter demo program

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.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()
Jul 18 '05 #1
5 2967
I can not get it to run.
S
"max(01)*" <ma**@fisso.casa> wrote in message
news:aQ***********************@news4.tin.it...
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.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()

Jul 18 '05 #2
max(01)* wrote:

i also used try-except for checking for invalid files.
[...]
i'd like to hear your suggestions and comments for improving it.


Without actually reading your code, if I may, I would suggest using
the os module to confirm that a file exists rather than waiting for an
error to be thrown.
userSuppliedPath = "/this/path/does/not/exist/"
import os.path
if not os.path.isfile( userSuppliedPath ): # this has the twofold purpose of:
# 1) making sure the path exists, and
# 2) making sure the path is a file
print "Error!"
Error!


--
Daniel Bickett
dbickett at gmail.com
http://heureusement.org/
Jul 18 '05 #3
Samantha wrote:
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()
Jul 18 '05 #4
Max,

Thanks and good job!

Harlin

Jul 18 '05 #5
Thanks that worked.
S
"max(01)*" <ma**@fisso.casa> wrote in message
news:oE***********************@news4.tin.it...
Samantha wrote:
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()

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: km | last post by:
Hi all, Is there any good online tutorial which is a good intro for Tkinter programming in Python ? regards, KM
2
by: Ringwraith | last post by:
Hello! How to detect in Python that some click action was performed in some module imported by my main module? Let's assume I have a tree.py module that implements all functionality of a tree...
7
by: SeeBelow | last post by:
Do many people think that wxPython should replace Tkinter? Is this likely to happen? I ask because I have just started learning Tkinter, and I wonder if I should abandon it in favor of...
6
by: max(01)* | last post by:
hi people. when i create a widget, such as a toplevel window, and then i destroy it, how can i test that it has been destroyed? the problem is that even after it has been destroyed, the instance...
0
by: | last post by:
I write a program with Python 2.4 + Tkinter Execute it, there will be a window show something. If I minimize it, it will be minimized to the taskbar. But I would like it to miniminze to the System...
6
by: Eric_Dexter | last post by:
Instead of creating my buttons and waiting for me to press them to execute they are executing when I create them and won't do my callback when I press them.. thanks for any help in advance ...
2
by: =?iso-8859-1?q?C=E9dric_Lucantis?= | last post by:
Hi, I can only load gif images with Tkinter.PhotoImage and none with BitmapImage. I tried png, jpg, bmp and xpm and always got this errors : Traceback (most recent call last): File...
8
by: karthikbalaguru | last post by:
Hi, One of my python program needs tkinter to be installed to run successfully. I am using Redhat 9.0 and hence tried installing by copying the tkinter-2.2.2-36.i386.rpm alone from the CD 3 to...
2
by: CDMAPoster | last post by:
I wrote a very simple demo program in A97 for concatenating email lists. The undo and redo buttons only undo or redo a set of single clicks, not a group operation. I might add a function to save...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.