[Tutor] help

geon geon at post.cz
Thu Aug 11 20:42:49 CEST 2005


Dan Deternova napsal(a):

> i am making a simple script to get the hang of Tkinter. i want to use 
> the input of the user from a Entry and calcuate the area. i have 
> tryied many way... as you can see in my script.   the Entry is under 
> def area():  and the output i want is under def cal():  ... please 
> help me fix my code and explain to me what you did.. thanks in advance.
>
> <mailto:%7Emoparfan90 at gmail.com>

here is corrected script, I just correct the area funciton and all what 
belongs to it. Instead of DoubleVar, you could also use IntVar, if you 
prefer....

#program made and maintained by Dan Deternova
#start of program

from Tkinter import 
*                                                                       
# tells computer we are using Tkinter modual
from tkMessageBox import *
def cal():
    win4 = Toplevel
    print 'area = ', w.get()*h.get()
   
def notyet():
    showerror('not yet avalable')

win = 
Tk()                                                                                  
# makes windows and has value for Tk
def makemenu(Tk):
    top = Menu(win)
    win.config(menu=top)

    file = Menu(top)
    file.add_command(label='save your name', command=newwin, underline=0)
    file.add_command(label='quit', command=win.destroy, underline=0)
    file.add_command(label='quit all', command=win.quit, underline=0)
    top.add_cascade(label='file', menu=file, underline=0)
    edit = Menu(top, tearoff=0)
    edit.add_command(label='copy', command=notyet, underline=0)
    edit.add_command(label='paste', command=notyet, underline=0)
    edit.add_command(label='quit', command=win.destroy, underline=0)
    top.add_cascade(label='edit', menu=edit, underline=0)


def 
newwin():                                                                               
# callback to define button newwin.
    win2 = 
Toplevel()                                                                       
# makes new window when button is pressed. named win2
    widget = Label(win2, text='your name 
here').pack()                                      # line 'type your 
name here' is printed on top of entry
    name = StringVar()
    widget = Entry(win2, 
textvariable=name).pack()                                          # 
makes entry in new window at the top.
    widget = Label(win2, text='age 
here').pack()                                            # prints the 
line 'age here'
    age = StringVar()
    widget = Entry(win2, 
textvariable=age).pack()                                           # 
makes entry
    widget = Label(win2, text='type your address here').pack()
    address = StringVar()
    widget = Entry(win2, textvariable=address).pack()
    def save():
         f=file('fname','a')
         f.write(name.get()+'\n')
         f.write(age.get()+'\n')
         f.write(address.get()+'\n')

    widget = Button(win2, text='save', command=save).pack(side=LEFT)
    widget = Button(win2, text='quit', 
command=win2.destroy).pack(side=RIGHT)
def area():
    global w,h
    win3 = Toplevel()
    widget = Label(win3, text='type hieght here: ').pack()
    h = DoubleVar()
    widget = Entry(win3, textvariable=h).pack()
    widget = Label(win3, text='type width here:  ').pack()
    w = DoubleVar()
    widget = Entry(win3, textvariable=w).pack()
    widget = Button(win3, text=' calculate ', 
command=cal).pack(side=BOTTOM, expand=YES, fill=BOTH)
  
   
fontentry = ('times', 20, 
'bold')                                                           # 
(font, size, style) defonision for fontentry. will be used later on in 
the program
widget = Entry(win, text='type 
here')                                                       # makes 
entry point in 'win'
makemenu(win)
widget.config(font=fontentry)                                                               
# makes the font of entry equal to 'fontentry'
widget.config(bg='blue', 
fg='yellow')                                                      # 
makes the background (bg) black and the forground (text) yellow
widget.pack(side=TOP, expand=YES, 
fill=BOTH)                                                # 'packs' the 
entry on top of win and expands and fill the Y axes
widget = Button(win, text="your name", command=newwin).pack(side=LEFT, 
expand=YES, fill=BOTH)# creates button that says 'your name'. see newwin 
callback for output.
widget = Button(win, text='quit all', 
command=win.quit).pack(side=BOTTOM, expand=YES, fill=BOTH)
widget = Button(win, text='quit', command=win.destroy).pack(side=RIGHT, 
expand=YES, fill=BOTH)
widget = Button(win, text='area', command=area).pack(side=LEFT, 
expand=YES, fill=BOTH)
win.title('my program')

#end of program

-- 
geon


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050811/10f38745/attachment.htm


More information about the Tutor mailing list