not stopping til it works

richard_chamberlain richard_chamberlain at ntlworld.com
Sun Jul 30 07:10:04 EDT 2000


Walter,

from Tkinter import *
root=Tk()
frame1=Frame(root)
frame2=Frame(root)
answer=StringVar()
ent1SV=StringVar()
ent2SV=StringVar()
label=Label(frame1,width=30,textvariable=answer)
entry1=Entry(frame2,width=20,textvariable=ent1SV)
entry2=Entry(frame2,width=20,textvariable=ent2SV)
button=Button(root,width=10,text='Calculate')
frame1.pack(side=TOP)
frame2.pack(side=TOP)
button.pack(side=TOP,anchor=NE)
label.pack(side=TOP)
entry1.pack(side=LEFT)
entry2.pack(side=LEFT)
def power(event):
    int1=int(ent1SV.get())
    int2=int(ent2SV.get())
    str="%d to the power of %d=%d" %(int1,int2,(int1**int2))
    answer.set(str)
button.bind('<Button>',power)
root.mainloop()

Not a very beautiful example so you'll need to mess around with the
structure.

To get the contents of entries use a StringVar. So you create an instance of
a StringVar and then assign it to a textvariable option. You then use .set
and .get to set and get the contents.

You can assign the button's event in two different ways, firstly by using
the command option and as I'm demonstrating here by binding an event to a
procedure.

Richard

walter hanagriff <walter111 at structurex.net> wrote in message
news:3983E403.36183DF8 at structurex.net...
> before i go on, one thing to say, i do not have the money to buy a book
> right now, otherwise i would, it would be great to actually be able to
> read a good tutorial on tkinter at almost any price, just saying since i
> did get that suggestion once
> i have tried so many ways to get what i want to work, and getting
> nowhere, its definitely not from lack of trying and patience that keeps
> me asking the same questions, though if i ever get these 2 answered it
> will be all for awhile i am sure
> i wont give up though so i have simplified the problem as much as
> possible, the only problems so far that i know of
> i have these 2 entry fields, how do i get any info that a person puts
> into them
> i tried using textvariable but i cant get it to work, and i tried using
> the get method with entry widget, but neither of these 2 things work
>
> next, how do i get that info to work with the pow() function, ex. the
> user inputs 3 in box 1 and 2 in box 2, i get those numbers and show on
> the screen this:
> first entry, " to the power of ", second entry, " = ", first
> entry**second entry
> actually it should look like this, "3 to the power of 2 = 9"
> i next want it so that when pressed, button will make this info appear
> in the frame
> or any better ideas for showing the answer?
> unfortunately i can do very little testing to see how to make the button
> work since i have a hard enough time getting there because of the first
> problem, but in test i have given predefined numbers to print out and
> tried using the button to show them and i tried not packing the label
> til i pressed button, or tried calling a def when i press button but no
> matter what the label prints out along with entry fields, before i even
> press the button and pressing button does nothing, when i dont even get
> an error from something
>
> def power(self, frame):
>     Entry(frame, width = 10).pack()
>     Entry(frame, width = 10).pack()
>     Button(frame, text = 'Get answer').pack(side = LEFT)
>
>





More information about the Python-list mailing list