By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,687 Members | 2,055 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,687 IT Pros & Developers. It's quick & easy.

Press button to load data

P: n/a
I'm new to Python, and programming in general.
What I'm trying to do here is to load a list of accounts from a file on
my harddrive into a string of Buttons in Tkinter, and when I press one
of the Buttons, which has one of my account name, it will load that
account into a new window. But I don't understand how to code the
proccess that would tell the program what account is selected. Any help
with this would be very appreciated. Thanks in advance.

from Tkinter import *
import shelve
from tkMessageBox import showerror

shelvename = shelve.open('class-shelve2')
cat = (' Name ', ' Account # ', ' Amount Due ', ' Date Due ')

def NameFields(top):
name1 = Label(None, text=cat[0], relief=RIDGE, width=20, fg='blue',
bg='white', font=('bold',15))
name2 = Label(None, text=cat[1], relief=RIDGE, width=15, fg='blue',
bg='white', font=('bold',15))
name3 = Label(None, text=cat[2], relief=RIDGE, width=15, fg='blue',
bg='white', font=('bold',15))
name4 = Label(None, text=cat[3], relief=RIDGE, width=15, fg='blue',
bg='white', font=('bold',15))
name1.grid(row=0, column=0, sticky=NSEW)
name2.grid(row=0, column=1, sticky=NSEW)
name3.grid(row=0, column=2, sticky=NSEW)
name4.grid(row=0, column=3, sticky=NSEW)
top.columnconfigure(0, weight=1)
top.columnconfigure(1, weight=1)
top.columnconfigure(2, weight=1)
top.columnconfigure(3, weight=1)
def DisplayBills(top):
c=0
for bill in shelvename:
bill1 = Button(None, text= shelvename[bill].name,
font=('bold',10), command=fetchRecord)
bill2 = Label(None, text= shelvename[bill].account,
relief=RIDGE, font=('bold',10))
bill3 = Label(None, text= shelvename[bill].paymentDue,
relief=RIDGE, font=('bold',10), fg='red')
bill4 = Label(None, text= shelvename[bill].dateDue,
relief=RIDGE, font=('bold',10))
bill1.grid(row=c, column=0, sticky=NSEW)
bill2.grid(row=c,column=1, sticky=NSEW)
bill3.grid(row=c,column=2, sticky=NSEW)
bill4.grid(row=c,column=3, sticky=NSEW)
c = c + 1

def fetchRecord():

top = Tk()

DisplayBills(top), NameFields(top)

mainloop()

Nov 16 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Just from a glance my thoughts are to
start with one file and build on it. Make
a class of it so you can loop it to use
it over for each record.
You wrote that the info was in a file on
the hd. If it is in a file on the hd, use the
open()
function, read from the file, only one record
and write the data to a list.

You can incorporate the
button option,

"command = CallSomeFunction",

to call a function that builds a window,
and loads the data into labels or
entry boxes.
If you are going to modify
the data, entry boxes allow you to
modify it and save it back to a
file.

Also, when using the open() function,
close it after you get the data you need.
otherwise you may experience
unexpected problems.

client = open('client', 'r')
client.read() (readline()) (readlines())
client.close()

jim-on-linux

http//:www.inqvista.com

On Wednesday 15 November 2006 23:20,
gi*********@msn.com wrote:
I'm new to Python, and programming in general.
What I'm trying to do here is to load a list of
accounts from a file on my harddrive into a
string of Buttons in Tkinter, and when I press
one of the Buttons, which has one of my account
name, it will load that account into a new
window. But I don't understand how to code the
proccess that would tell the program what
account is selected. Any help with this would
be very appreciated. Thanks in advance.

from Tkinter import *
import shelve
from tkMessageBox import showerror

shelvename = shelve.open('class-shelve2')
cat = (' Name ', ' Account # ', ' Amount Due ',
' Date Due ')

def NameFields(top):
name1 = Label(None, text=cat[0],
relief=RIDGE, width=20, fg='blue', bg='white',
font=('bold',15))
name2 = Label(None, text=cat[1],
relief=RIDGE, width=15, fg='blue', bg='white',
font=('bold',15))
name3 = Label(None, text=cat[2],
relief=RIDGE, width=15, fg='blue', bg='white',
font=('bold',15))
name4 = Label(None, text=cat[3],
relief=RIDGE, width=15, fg='blue', bg='white',
font=('bold',15))
name1.grid(row=0, column=0, sticky=NSEW)
name2.grid(row=0, column=1, sticky=NSEW)
name3.grid(row=0, column=2, sticky=NSEW)
name4.grid(row=0, column=3, sticky=NSEW)
top.columnconfigure(0, weight=1)
top.columnconfigure(1, weight=1)
top.columnconfigure(2, weight=1)
top.columnconfigure(3, weight=1)
def DisplayBills(top):
c=0
for bill in shelvename:
bill1 = Button(None, text=
shelvename[bill].name, font=('bold',10),
command=fetchRecord) bill2 = Label(None, text=
shelvename[bill].account, relief=RIDGE,
font=('bold',10))
bill3 = Label(None, text=
shelvename[bill].paymentDue, relief=RIDGE,
font=('bold',10), fg='red') bill4 = Label(None,
text= shelvename[bill].dateDue, relief=RIDGE,
font=('bold',10))
bill1.grid(row=c, column=0,
sticky=NSEW) bill2.grid(row=c,column=1,
sticky=NSEW) bill3.grid(row=c,column=2,
sticky=NSEW) bill4.grid(row=c,column=3,
sticky=NSEW) c = c + 1

def fetchRecord():

top = Tk()

DisplayBills(top), NameFields(top)

mainloop()
Nov 16 '06 #2

P: n/a

Without being able to run the code my question is
where is the id in the lambda defined?
On Thursday 16 November 2006 22:31, jim wrote:
Thanks for your help, but now I have a another
problem so here is my code again
when I run this it prints <built-in function
id>

from Tkinter import *
import shelve
from tkMessageBox import showerror

shelvename = shelve.open('class-shelve2')
cat = (' Name ', ' Account # ', ' Amount Due ',
' Date Due ')

def NameFields(top):
name1 = Label(None, text=cat[0],
relief=RIDGE, width=20, fg='blue', bg='white',
font=('bold',15))
name2 = Label(None, text=cat[1],
relief=RIDGE, width=15, fg='blue', bg='white',
font=('bold',15))
name3 = Label(None, text=cat[2],
relief=RIDGE, width=15, fg='blue', bg='white',
font=('bold',15))
name4 = Label(None, text=cat[3],
relief=RIDGE, width=15, fg='blue', bg='white',
font=('bold',15))
name1.grid(row=0, column=0, sticky=NSEW)
name2.grid(row=0, column=1, sticky=NSEW)
name3.grid(row=0, column=2, sticky=NSEW)
name4.grid(row=0, column=3, sticky=NSEW)
top.columnconfigure(0, weight=1)
top.columnconfigure(1, weight=1)
top.columnconfigure(2, weight=1)
top.columnconfigure(3, weight=1)
def DisplayBills(top):
c=0
x = []
global bill
for bill in shelvename:
global funcs
bill1 = Button(None, text=
shelvename[bill].name,
font=('bold',10),command=(lambda x = id:
fetchRecord(x)))

bill2 = Label(None, text=
shelvename[bill].account, relief=RIDGE,
font=('bold',10))
bill3 = Label(None, text=
shelvename[bill].paymentDue, relief=RIDGE,
font=('bold',10), fg='red') bill4 = Label(None,
text= shelvename[bill].dateDue, relief=RIDGE,
font=('bold',10))
bill1.grid(row=c, column=0,
sticky=NSEW) bill2.grid(row=c,column=1,
sticky=NSEW) bill3.grid(row=c,column=2,
sticky=NSEW) bill4.grid(row=c,column=3,
sticky=NSEW) c = c + 1
return bill

def fetchRecord(x):
print x

top = Tk()

DisplayBills(top), NameFields(top)

mainloop()

jim-on-linux wrote:
Just from a glance my thoughts are to
start with one file and build on it. Make
a class of it so you can loop it to use
it over for each record.
You wrote that the info was in a file on
the hd. If it is in a file on the hd, use the
open()
function, read from the file, only one record
and write the data to a list.

You can incorporate the
button option,

"command = CallSomeFunction",

to call a function that builds a window,
and loads the data into labels or
entry boxes.
If you are going to modify
the data, entry boxes allow you to
modify it and save it back to a
file.

Also, when using the open() function,
close it after you get the data you need.
otherwise you may experience
unexpected problems.

client = open('client', 'r')
client.read() (readline()) (readlines())
client.close()

jim-on-linux

http//:www.inqvista.com
Nov 17 '06 #3

P: n/a
On Friday 17 November 2006 02:58, you wrote:
On Fri, 17 Nov 2006 00:25:39 -0500,
jim-on-linux <in*****@verizon.net>

declaimed the following in comp.lang.python:
Without being able to run the code my
question is where is the id in the lambda
defined?

Please take into account that I've not
actually used lambdas, so might have some
mistakes in the syntax...
for bill in shelvename:
global funcs
bill1 = Button(None, text=
shelvename[bill].name,
font=('bold',10),command=(lambda x = id:
fetchRecord(x)))

"id" would be something that "identifies" the
button... In this case, maybe you can use
"bill":
Think about relating a Tkinter variable to each
button then the button is related to a unique
variable. ( Tkinter StingVar or IntVar or some
others.) Then you will have to keep the variables
in a list or dictionary for recalling.
jim-on-linux
http://www.inqvista.com


... command=(lambda x = bill: fetchRecord(x))
...

As I understand the lambda syntax, what this
does is create a "function" (which is the
command that gets run when the button is
pushed), and this function will call
fetchRecord passing it the value that "x" had
at the time of definition (hence the x=...)
--
Wulfraed Dennis Lee Bieber KD6MOG
wl*****@ix.netcom.com wu******@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support
Staff: we******@bestiaria.com)
HTTP://www.bestiaria.com/
Nov 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.