473,703 Members | 2,472 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.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 ()
Jul 18 '05 #1
5 3003
I can not get it to run.
S
"max(01)*" <ma**@fisso.cas a> 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.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 ()

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.
userSuppliedPat h = "/this/path/does/not/exist/"
import os.path
if not os.path.isfile( userSuppliedPat h ): # 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.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 ()
Jul 18 '05 #4
Max,

Thanks and good job!

Harlin

Jul 18 '05 #5
Thanks that worked.
S
"max(01)*" <ma**@fisso.cas a> 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.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 ()

Jul 18 '05 #6

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

Similar topics

4
407
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
1519
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 view widget and my sample application (let's say myapp.py) that imports those tree module to create tree view control inside the main frame. I want in my application to have some method that responds immediately (!!!!) to click action performed...
7
11900
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 wxPython. Mitchell Timin -- "Many are stubborn in pursuit of the path they have chosen, few in
6
17983
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 still exists and has a tkinter name, so testing for None is not feasible: >>> import Tkinter >>> fin = None >>> fin1 = Tkinter.Toplevel()
0
2518
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 Tray, this can make taskbar more clear. Would you please tell me how to modify my program. Thanks a lot !! Soure Code :
6
1877
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 button = Tkinter.Button(frame,text = returnstring, command=callback(returnstring))# this line executes on creation my output on startup is (should output when I choose an option)
2
5517
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 "<stdin>", line 1, in ? File "/usr/lib/python2.4/lib-tk/Tkinter.py", line 3206, in __init__ Image.__init__(self, 'photo', name, cnf, master, **kw) File "/usr/lib/python2.4/lib-tk/Tkinter.py", line 3162, in __init__
8
3286
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 my pc. But, it is not getting installed and is failing by throwing the below errors. Should i need to configure / install any specific files for resolving this issue ?
2
1538
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 the table state (into a table with similar field names) so that clearing or selecting all the addresses can be undone as well. Later I should add the ability to save and recall named lists and to add all the users within a specific category. ...
0
8672
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9123
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6594
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5923
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4434
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3125
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2070
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.