Re: tkinter, canvas, get color of image?

2008-04-18 Thread skanemupp
On 16 Apr, 00:46, Bryan Oakley [EMAIL PROTECTED] wrote:
 [EMAIL PROTECTED] wrote:
  On 13 Apr, 19:19, Bryan Oakley [EMAIL PROTECTED] wrote:
  [EMAIL PROTECTED] wrote:
  mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\elmapovic.gif')
  w.create_image(10, 10, image = mapq, anchor = NW)
  after doing this is there any possibility of getting the
  characteristics of the GIF-picture(or bitmap if i use that)?
  it would be very helpfull if i for example could do something like
  canvas.getcolor(image, mouseclick.x,mouseclick.y) if u get the point.
  get the color of the image where i clicked.
  The image isn't painted on the canvas, so to answer your specific
  question, no, you can't get the color of a pixel on the canvas in the
  way that you ask.

  However, when you click on the canvas you can get the item that was
  clicked on and the x/y of the click. From that you can figure out which
  pixel of the image is under the cursor. And from that you can query the
  image for the color of a specific pixel.

  how do i get the item?
 http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_image-...

  with any of those methods?

  when i click the mouse i can get event.object u mean?

 You can use find_closest to find the object closest to the x,y of the
 event. You can also do find_withtag(tk.CURRENT) which returns the item
 under the mouse pointer.


Exception in Tkinter callback
Traceback (most recent call last):
  File C:\Python25\lib\lib-tk\Tkinter.py, line 1403, in __call__
return self.func(*args)
  File C:\Users\saftarn\Desktop\pythonkod\mapexperiments
\mapgetobject.py, line 17, in callback
undermouse=find_closest(master.CURRENT)
NameError: global name 'find_closest' is not defined


from Tkinter import *

master = Tk()

w = Canvas(master, width=400, height=625)
w.pack(expand = YES, fill = BOTH)

mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\images
\something.gif')
w.create_image(30, 30, image = mapq, anchor = NW)

def key(event):
print pressed, repr(event.char)

def callback(event):
clobj=event.widget
##undermouse=find_withtag(master.CURRENT)
undermouse=find_closest(master.CURRENT)
print repr(undermouse)

w.bind(Key, key)
w.bind(Button-1, callback)
w.pack()

mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


Tkinter, getting canvas-object, how?

2008-04-18 Thread skanemupp
so i load a gif onto a canvas and when i click the canvs i want to get
the color of the pixel that is clicked.
so i need to ge the object im clicking.
i was told in another thread to use find_withtag or find_closest but
it is not working, maybe im using the
method on the wrong object.
how do i do this?
and how do i then get specifics about that object, ie the pixel-color?

Exception in Tkinter callback
Traceback (most recent call last):
  File C:\Python25\lib\lib-tk\Tkinter.py, line 1403, in __call__
return self.func(*args)
  File C:\Users\saftarn\Desktop\pythonkod\mapexperiments
\mapgetobject.py, line 17, in callback
undermouse=find_closest(master.CURRENT)
NameError: global name 'find_closest' is not defined

from Tkinter import *

master = Tk()

w = Canvas(master, width=400, height=625)
w.pack(expand = YES, fill = BOTH)

mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\Maps\provinces-of-
sweden.gif')
w.create_image(30, 30, image = mapq, anchor = NW)

def key(event):
print pressed, repr(event.char)

def callback(event):
clobj=event.widget
##undermouse=find_withtag(master.CURRENT)
undermouse=find_closest(master.CURRENT)
print repr(undermouse)

w.bind(Key, key)
w.bind(Button-1, callback)
w.pack()

mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


Easiest way to get started with WebApps?

2008-04-18 Thread skanemupp
which is the easiest module to use to just get started with webapps
quicklya nd starting getting things up and running, not advanced stuff
just basic.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: def power, problem when raising power to decimals

2008-04-17 Thread skanemupp
actually that 0**0 statement was wrong. 0**0 = 1 and should be.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: py2exe, program has stoped working!?

2008-04-16 Thread skanemupp
On 14 Apr, 14:11, John Machin [EMAIL PROTECTED] wrote:
 On Apr 14, 7:24 pm, [EMAIL PROTECTED] wrote:



   Is it a console program or a gui program?
  GUI
   What happens when you run it without py2exe?

  it works perfectly, both from within python and launching from
  windows

   Have you searched for has stopped working in

  (a) your source code
  yes no such message there (b) the py2exe source code?

  no, will do but doubt thats the problem

   Have you managed to get any py2exe-created program to run properly?

  no

 Well, perhaps you might like to look in the samples directory of the
 py2exe distribution and choose a simple example and try that.

 By the way, popup is what you get in a web browser. What did this
 popup look like: a panel from your GUI software? A Windows message
 box? Did it have a title across the top? What was the exact text in
 the popup/panel/box? Were there any options other than to close the
 window?

 Which version of Python? Which Windows, what service pack? What GUI,
 what version? Care to divulge the contents of your setup.py? Apart
 from your GUI, what 3rd party packages/modules are you importing?


its a windows message as others have said.
only option to close the window.

2.5python
windows vista
tkinter GUI

im importing tkinter and from future import division
-- 
http://mail.python.org/mailman/listinfo/python-list


Profiling very small/quick functions, help!?

2008-04-16 Thread skanemupp
i use this code to profile. however for small standard functions it
just says 0 seconds.
so one solution is to run the function a very big number of times(how
big?).
but the bottom code doesnt work, it just runs the same profiling 1
times insetad of running the fucntion 10K
times and evaluate the time of all thsoe 10K times.

ao how to solve this?

if __name__==__main__:
try:
from cProfile import run
except:
from profile import run
run(tests())

if __name__==__main__:
try:
from cProfile import run
except:
from profile import run
for x in range(1, 1):
run(power(10,10))
-- 
http://mail.python.org/mailman/listinfo/python-list


Profiling, recursive func slower than imperative, normal?

2008-04-16 Thread skanemupp
the 0.409 vs 0.095 is the total times right?
so the imperative function is 4 times faster than the recursive.
or what does tottime stand for?

is this always the case that the recursive function is slower?
the gain is less code?

are some functions only implementable recursively?



def power(nbr, po):
if po==0:
return 1
if po0:
return nbr*power(nbr, po-1)
if po0:
return 1/power(nbr, -1*po)

109992 function calls (10002 primitive calls) in 0.409 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall
filename:lineno(function)
10.0150.0150.4090.409 pyshell#217:1(test1)
10.0000.0000.4090.409 string:1(module)
109989/0.3940.0000.3940.000 myMath.py:39(power)
10.0000.0000.0000.000 {method 'disable' of
'_lsprof.Profiler' objects}



def power2(nbr, po):
acc=1
if po = 1:
acc=nbr
for x in range(1, po):
acc=acc*nbr
if po  0:
if nbr!=0:
acc=1
for x in range(0, po, -1):
acc=acc/nbr
else:
return Division by zero
return acc


20001 function calls in 0.095 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall
filename:lineno(function)
10.0260.0260.0950.095 pyshell#221:1(test1)
10.0000.0000.0950.095 string:1(module)
 0.0510.0000.0690.000 myMath.py:47(power2)
10.0000.0000.0000.000 {method 'disable' of
'_lsprof.Profiler' objects}
 0.0170.0000.0170.000 {range}
-- 
http://mail.python.org/mailman/listinfo/python-list


def power, problem when raising power to decimals

2008-04-16 Thread skanemupp
how do i solve power(5,1.3)?

def power(nbr, po):
if po==0:
return 1
if po0:
return nbr*power(nbr, po-1)
if po0:
return 1/power(nbr, -1*po)


also i found a link which states 0^0 isnt 1 even though every
calculator ive tried says it is.
it doesnt say what it is but i presume 0 then.
but it seems the dude is wrong and it is 1?


dont run the code with decimals, it will never leave the function, u
have to restart the shell(if using the standard python ide)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tkinter, canvas, get color of image?

2008-04-15 Thread skanemupp
On 13 Apr, 19:19, Bryan Oakley [EMAIL PROTECTED] wrote:
 [EMAIL PROTECTED] wrote:
  mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\elmapovic.gif')
  w.create_image(10, 10, image = mapq, anchor = NW)

  after doing this is there any possibility of getting the
  characteristics of the GIF-picture(or bitmap if i use that)?

  it would be very helpfull if i for example could do something like
  canvas.getcolor(image, mouseclick.x,mouseclick.y) if u get the point.
  get the color of the image where i clicked.

 The image isn't painted on the canvas, so to answer your specific
 question, no, you can't get the color of a pixel on the canvas in the
 way that you ask.

 However, when you click on the canvas you can get the item that was
 clicked on and the x/y of the click. From that you can figure out which
 pixel of the image is under the cursor. And from that you can query the
 image for the color of a specific pixel.


how do i get the item?
http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_image-method

with any of those methods?

when i click the mouse i can get event.object u mean?
-- 
http://mail.python.org/mailman/listinfo/python-list


tkinter, event.widget, what do i get?

2008-04-15 Thread skanemupp
when calling function hmm here, what do i get? the widget i clicked
on?
if i have a canvs on wich i have a bitmap and i click on the bitmap,
is the event.widget then the bitmap?
can i get info about the bitmap then? like color of the pixel i
clicked. if so, how?


w.bind(Key, key)
w.bind(Button-1, hmm)

def hmm(event):
return event.widget
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tkinter, event.widget, what do i get?

2008-04-15 Thread skanemupp
On 16 Apr, 00:24, Gabriel Genellina [EMAIL PROTECTED] wrote:
 En Tue, 15 Apr 2008 17:45:08 -0300, [EMAIL PROTECTED] escribió:

  when calling function hmm here, what do i get? the widget i clicked
  on?
  if i have a canvs on wich i have a bitmap and i click on the bitmap,
  is the event.widget then the bitmap?
  can i get info about the bitmap then? like color of the pixel i
  clicked. if so, how?

  w.bind(Key, key)
  w.bind(Button-1, hmm)

  def hmm(event):
  return event.widget

 Why don't you try by yourself? You can use: print repr(something)

 --
 Gabriel Genellina


i get Tkinter.Canvas instance at 0x01B9B6E8

thing is i get that even though i click outside the image.
and what can i do with this number anyway?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: py2exe, program has stoped working!?

2008-04-14 Thread skanemupp
 Is it a console program or a gui program?
GUI
 What happens when you run it without py2exe?
it works perfectly, both from within python and launching from
windows

 Have you searched for has stopped working in
(a) your source code
yes no such message there
 (b) the py2exe source code?
no, will do but doubt thats the problem

 Have you managed to get any py2exe-created program to run properly?
no

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Game design : Making computer play

2008-04-14 Thread skanemupp
On 14 Apr, 09:13, v4vijayakumar [EMAIL PROTECTED]
wrote:
 In computer based, two player, board games, how to make computer play?
 Are there any formal ways to _teach_ computer, to choose best possible
 move?

 I know this is kind of off-topic here. Please redirect me, if there
 are more appropriate newsgroup.

 Many thanks.

can you post a link to the game so I can see the rules and how the
board looks.


/hopefully going to india soon
-- 
http://mail.python.org/mailman/listinfo/python-list


latest command from toplevel?

2008-04-13 Thread skanemupp
windows vista and python 2.5, is there a way to get the latest command
entered? would be very useful.

if not by default, anything i could download or write myself?
-- 
http://mail.python.org/mailman/listinfo/python-list


py2exe, program has stoped working!?

2008-04-13 Thread skanemupp
so i used py2exe and i have the build and the dist-folders.

in the distfolder there is a Calculator.exe file.

when i run it it just says Calculator.exe has stopped working in a
popup but the program itself never shows up.


wtf!?

and when im distributing my program i have to include both catalogues
right?
-- 
http://mail.python.org/mailman/listinfo/python-list


Tkinter, image not appearing in function but without function

2008-04-13 Thread skanemupp
why is the first program not working? when i click the screen the map
is not appearing.

the second program works.



from Tkinter import *

master = Tk()

w = Canvas(master, width=700, height=600)
w.pack(expand = YES, fill = BOTH)

def mapper():
mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\world-map.gif')
w.create_image(10, 10, image = mapq, anchor = NW)

def key(event):
print pressed, repr(event.char)

def callback(event):
w.focus_set()
print clicked at, event.x, event.y
mapper()
print 'yo'
square = w.create_oval(event.x-5,event.y-5,event.x+5,event.y+5,
fill=black)

w.bind(Key, key)
w.bind(Button-1, callback)
w.pack()


mainloop()




from Tkinter import *

master = Tk()


w = Canvas(master, width=700, height=600)
w.pack(expand = YES, fill = BOTH)

q=raw_input(Choose: i=India, s=sweden, else World: )
if q==i:
mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\map-india.bmp')
elif q=='s':
mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\provinces-of-
sweden.gif')
else:
mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\world-map.gif')

print mapq.height(), mapq.width()
w.create_image(10, 10, image = mapq, anchor = NW)

def key(event):
print pressed, repr(event.char)

def callback(event):
w.focus_set()
print clicked at, event.x, event.y
square = w.create_oval(event.x-5,event.y-5,event.x+5,event.y+5,
fill=black)

hur hitta om inom ratt-kolla color of 

w.bind(Key, key)
w.bind(Button-1, callback)
w.pack()

mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


tkinter, canvas, get color of image?

2008-04-13 Thread skanemupp
mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\elmapovic.gif')
w.create_image(10, 10, image = mapq, anchor = NW)

after doing this is there any possibility of getting the
characteristics of the GIF-picture(or bitmap if i use that)?

it would be very helpfull if i for example could do something like
canvas.getcolor(image, mouseclick.x,mouseclick.y) if u get the point.
get the color of the image where i clicked.
-- 
http://mail.python.org/mailman/listinfo/python-list


tkinter, editing an entry, int-value of insert?

2008-04-12 Thread skanemupp
in this program when using the c-button it deletes the last token
entered. i want to delete the token after the mousecursor.

lets say the string is: 12*(81**.5+12) and i put the cursor between
the * and * because i want times .5 not root.
now if i press c it deletes ) which is not what i want. ive tried
coming up with a function that does what i want
but neither of the ways as shown below works.
i want to do something like e.delete(INSERT, INSERT+1) but that doesnt
work because it is string+int and
INSERT+1 doesnt work either. so how do i get the int-value of
insert?
what is the trick?


def Backspace():
a=len(e.get())
e.delete(a-1,END)
#e.delete(INSERT, END)
#e.delete(ANCHOR,END)




from __future__ import division
import Tkinter
from Tkinter import *

mygui = Tkinter.Tk()

mygui.title(Calculator)

e = Entry(mygui)
e.grid(row=1, column=1, columnspan=4, sticky=NSEW)

c = Entry(mygui)
c.grid(row=2, column=1, columnspan=4, sticky=NSEW)

def Disp(nstr):
e.insert(INSERT, nstr)

def Calc():
expr=e.get()
c.delete(0, END)
try:
c.insert(END, eval(expr))
except:
c.insert(END, Not computable)

def Erase():
e.delete(0,END)
c.delete(0, END)

def Backspace():
a=len(e.get())
e.delete(a-1,END)

x = 1
y = 4
for char in '123+456-789*0()/.':
b = Button(mygui, text=char, command=lambda n=char:Disp(n),
width=2, height=1)
b.grid(row=y, column=x, sticky=NSEW)
x=x+1
if x==5:
x=1
y=y+1

b = Button(mygui, text=^, command=lambda n=**:Disp(n), width=2,
height=1)
b.grid(row=8, column=2, sticky=NSEW)
b = Button(mygui, text=C,command=Erase, width=2, height=1)
b.grid(row=8, column=3, sticky=NSEW)
b = Button(mygui, text=c,command=Backspace, width=2, height=1)
b.grid(row=8, column=4, sticky=NSEW)
b = Button(mygui, text==,command=Calc, width=18, height=1)
b.grid(row=9, column=1, columnspan=4, sticky=NSEW)

mygui.mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tkinter, editing an entry, int-value of insert?

2008-04-12 Thread skanemupp
solved this:

def Backspace():
st = e.index(INSERT)
e.delete(st-1,st)
-- 
http://mail.python.org/mailman/listinfo/python-list


toplevel, get latest entry?

2008-04-12 Thread skanemupp
windows vista and python 2.5, is there a way to get the latest command
entered? would be very useful.

if not by default, anything i could download or write myself?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tkinter, overwrite Label-text?

2008-04-11 Thread skanemupp
On 10 Apr, 18:03, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote:
 On Thu, 10 Apr 2008 07:37:08 -0700, skanemupp wrote:
  i know how to do this already. the problem is i want the text to stay
  in the windowa nd not start overwriting Answer:.

 Then don't use `place()` but let Tkinter handle the layout with the pack
 and/or grid layout manager.  GUIs with `place()` are a bad idea because
 the GUI may look odd or is even unusable on other peoples computers with
 other screen resolutions, fonts, and font sizes.

 Ciao,
 Marc 'BlackJack' Rintsch

ok but i have trouble using grid. if i try to use a Label witht he
text answer in the following code it doenst work very well.


from __future__ import division
import Tkinter
from Tkinter import *

mygui = Tkinter.Tk()

mygui.title(Calculator)

##l = Label(mygui, text=Answer: )
##l.grid(row=2, column=1, columnspan=2)

e = Entry(mygui)
e.grid(row=1, column=1, columnspan=4)

c = Entry(mygui)
c.grid(row=2, column=1, columnspan=4)

def Disp(nstr):
e.insert(INSERT, nstr)

def Calc():
expr=e.get()
c.delete(0, END)
try:
c.insert(END, eval(expr))
except:
c.insert(END, Not computable)

def Erase():
e.delete(0,END)
c.delete(0, END)

def Backspace():
a=len(e.get())
e.delete(a-1,END)
#e.delete(INSERT, END)
#e.delete(ANCHOR,END)


x = 1
y = 4
for char in '123+456-789*0()/.':
b = Button(mygui, text=char, command=lambda n=char:Disp(n),
width=2, height=1)
b.grid(row=y, column=x)
x=x+1
if x==5:
x=1
y=y+1

b = Button(mygui, text=^, command=lambda n=**:Disp(n), width=2,
height=1)
b.grid(row=8, column=2)
b = Button(mygui, text=C,command=Erase, width=2, height=1)
b.grid(row=8, column=3)
b = Button(mygui, text=c,command=Backspace, width=2, height=1)
b.grid(row=8, column=4)
b = Button(mygui, text==,command=Calc, width=18, height=1)
b.grid(row=9, column=1, columnspan=8)

mygui.mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


Profiling programs/scripts?

2008-04-11 Thread skanemupp
how do i profile a program? i found out that there are some profilers
included in the standard library but couldnt really figure out how to
access/use them

-- 
http://mail.python.org/mailman/listinfo/python-list


tkinter, annoying grid-problem

2008-04-11 Thread skanemupp
so my little calculator works perfectly now. just having some trouble
with the layout.
this whole tkinter-thing seems to be more tricky than it should be.
how can i make the 4 column of buttons have the same distance and
size  between them as the other 3 columns?
and how can i make the top entry end where the 2nd row entry
ends(meaning the top entry will be longer)?

why are the 4th row split from the others? hard to fix the problems
when u dont even understand why things happen. seems so llogical a lot
of it. i change something then something unexpected happens.

from __future__ import division
import Tkinter
from Tkinter import *

mygui = Tkinter.Tk()

mygui.title(Calculator)

l = Label(mygui, text=Answer: )
l.grid(row=2, column=1, columnspan=2, sticky=W)

e = Entry(mygui)
e.grid(row=1, column=1, columnspan=4, sticky=W)

c = Entry(mygui)
c.grid(row=2, column=3, columnspan=4, sticky=W)

def Disp(nstr):
e.insert(INSERT, nstr)

def Calc():
expr=e.get()
c.delete(0, END)
try:
c.insert(END, eval(expr))
except:
c.insert(END, Not computable)

def Erase():
e.delete(0,END)
c.delete(0, END)

def Backspace():
a=len(e.get())
e.delete(a-1,END)
#e.delete(INSERT, END)
#e.delete(ANCHOR,END)


x = 1
y = 4
for char in '123+456-789*0()/.':
b = Button(mygui, text=char, command=lambda n=char:Disp(n),
width=2, height=1)
b.grid(row=y, column=x, sticky=W)
x=x+1
if x==5:
x=1
y=y+1

b = Button(mygui, text=^, command=lambda n=**:Disp(n), width=2,
height=1)
b.grid(row=8, column=2, sticky=W)
b = Button(mygui, text=C,command=Erase, width=2, height=1)
b.grid(row=8, column=3, sticky=W)
b = Button(mygui, text=c,command=Backspace, width=2, height=1)
b.grid(row=8, column=4, sticky=W)
b = Button(mygui, text==,command=Calc, width=18, height=1)
b.grid(row=9, column=1, columnspan=4, sticky=W)

mygui.mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


questions about Exceptions?

2008-04-10 Thread skanemupp
is there a general philosophy as to when to use exceptions and when
not to?

like here:
def Calc():
global nbr
try:
print eval(nbr)
except:
print Not computable
nbr = 

i have a calculator and nbr is a string that contains '0123456789+-*/'

if the string ends on +-*/ it will throw an exception(unexpected EOF).

i could easily prevent the exceptions here with an if-statement, is
that preferrable and why?


also when u throw exceptions should u catch the speicfic one? i guess
only if u want it to do soemthing special since  catching only only
one exception logicall would abort the program if another one is
thrown?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: questions about Exceptions?

2008-04-10 Thread skanemupp
On 10 Apr, 12:38, [EMAIL PROTECTED] wrote:
  def Calc():
  global nbr
  try:
  print eval(nbr)
  #a = Label(mygui, text=eval(nbr))
  #a.place(relx=0.4, rely=0.1, anchor=CENTER)
  except:
  print Not computable
  nbr = 

  def Erase():
  global nbr
  nbr = 

 Seems to me you could be better off passing a parameter and a return
 statement of None (or your parameter cleaned) for those functions,
 which should work. Given an input, Eval it and then return None. That
 way you wouldn't need the Erase...

the erase() id alwys need if u wanna abort whilst u wrote something.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: questions about Exceptions?

2008-04-10 Thread skanemupp
On 10 Apr, 10:51, [EMAIL PROTECTED] wrote:
 In general you should only catch the exceptions you want to catch,
 therefore avoiding the issue of catching unexpected ones, for
 instances the programming unexpectandly closing.

 Well, exception handling is expensive (when it catches one) so it
 really is up to you. If you are using eval and know it might EOF
 then you should probably look to handle that. The main IF statement
 style I can think of (checking the end of the string) wouldn't be much
 of an improvement.

 Currently I would be very worried about seeing that code as it breaks
 a number of conventions. However it depends on the importance of the
 code to wherever or not you should change this. (Global variable, the
 use of Eval, the CATCH ALL except and the setting of a global variable
 at the end.)

 I've seen a good few (simple and advanced) calculator examples using
 python on the NET, it might be worth looking at some to see their
 style of coding a calculator to help your own.


i know about the GLOBAL stuff, i might rewrite the program using
classes later so i can avoid this. i am mainly playin with tkinter for
now.

i was thinking the same check with if at the beginning and end of the
string so there isnt a */ but also if someone uses /// or soemthing
like that in the middle it crashes so it is hard to cover every case,
esp since **5 means ^5.
is the use of if also expensive?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: questions about Exceptions?

2008-04-10 Thread skanemupp
here is the whole code:

from __future__ import division
import Tkinter
from Tkinter import *

mygui = Tkinter.Tk()
mygui.title(Calculator)

w = Label(mygui, text=Answer: )
w.place(relx=0.15, rely=0.1, anchor=CENTER)

nbr = 

def Disp(nstr):
global nbr
nbr=nbr+nstr
print You need to seek help!,nbr

def Calc():
global nbr
try:
print eval(nbr)
#a = Label(mygui, text=eval(nbr))
#a.place(relx=0.4, rely=0.1, anchor=CENTER)
except:
print Not computable
nbr = 

def Erase():
global nbr
nbr = 


b = Button(mygui, text=1,command=lambda n='1':Disp(n), width=2,
height=1)
b.place(relx=0.1, rely=0.2, anchor=CENTER)
b = Button(mygui, text=2,command=lambda n='2':Disp(n), width=2,
height=1)
b.place(relx=0.2, rely=0.2, anchor=CENTER)
b = Button(mygui, text=3,command=lambda n='3':Disp(n), width=2,
height=1)
b.place(relx=0.3, rely=0.2, anchor=CENTER)
b = Button(mygui, text=+,command=lambda n='+':Disp(n), width=2,
height=1)
b.place(relx=0.4, rely=0.2, anchor=CENTER)
b = Button(mygui, text=4,command=lambda n='4':Disp(n), width=2,
height=1)
b.place(relx=0.1, rely=0.3, anchor=CENTER)
b = Button(mygui, text=5,command=lambda n='5':Disp(n), width=2,
height=1)
b.place(relx=0.2, rely=0.3, anchor=CENTER)
b = Button(mygui, text=6,command=lambda n='6':Disp(n), width=2,
height=1)
b.place(relx=0.3, rely=0.3, anchor=CENTER)
b = Button(mygui, text=-,command=lambda n='-':Disp(n), width=2,
height=1)
b.place(relx=0.4, rely=0.3, anchor=CENTER)
b = Button(mygui, text=7,command=lambda n='7':Disp(n), width=2,
height=1)
b.place(relx=0.1, rely=0.4, anchor=CENTER)
b = Button(mygui, text=8,command=lambda n='8':Disp(n), width=2,
height=1)
b.place(relx=0.2, rely=0.4, anchor=CENTER)
b = Button(mygui, text=9,command=lambda n='9':Disp(n), width=2,
height=1)
b.place(relx=0.3, rely=0.4, anchor=CENTER)
b = Button(mygui, text=*,command=lambda n='*':Disp(n), width=2,
height=1)
b.place(relx=0.4, rely=0.4, anchor=CENTER)
b = Button(mygui, text=0,command=lambda n='0':Disp(n), width=2,
height=1)
b.place(relx=0.1, rely=0.5, anchor=CENTER)
b = Button(mygui, text=C,command=Erase, width=2, height=1)
b.place(relx=0.2, rely=0.5, anchor=CENTER)
b = Button(mygui, text=^.5,command=lambda n='**.5':Disp(n), width=2,
height=1)
b.place(relx=0.3, rely=0.5, anchor=CENTER)
b = Button(mygui, text=/,command=lambda n='/':Disp(n), width=2,
height=1)
b.place(relx=0.4, rely=0.5, anchor=CENTER)
b = Button(mygui, text==,command=Calc, width=2, height=1)
b.place(relx=0.2, rely=0.7, anchor=CENTER)

mygui.mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: questions about Exceptions?

2008-04-10 Thread skanemupp
On 10 Apr, 13:15, [EMAIL PROTECTED] wrote:
  the erase() id alwys need if u wanna abort whilst u wrote something.

 But if it is meant to only evaluate once you've pressed the enter key
 (I take it?) you shouldn't need that. And if you are to abort while
 evaluating it will not do that.


CHANGED IT NOW TO A MUCH BETTER SOLUTION(will see if i can make a
better solution with the buttons, hate big blocks of similarlooking
code):


from __future__ import division
import Tkinter
from Tkinter import *

mygui = Tkinter.Tk()
mygui.title(Calculator)

l = Label(mygui, text=Answer: )
l.place(relx=0.15, rely=0.2, anchor=CENTER)

e = Entry(mygui)
e.place(relx=0.4, rely=0.1, anchor=CENTER)

def Disp(nstr):
e.insert(END, nstr)

def Calc():
expr=e.get()
try:
b = Label(mygui, text=eval(expr))
b.place(relx=0.4, rely=0.2, anchor=CENTER)
except:
b = Label(mygui, text=Not computable)
b.place(relx=0.4, rely=0.2, anchor=CENTER)

def Erase():
e.delete(0,END)

b = Button(mygui, text=1,command=lambda n='1':Disp(n), width=2,
height=1)
b.place(relx=0.1, rely=0.4, anchor=CENTER)
b = Button(mygui, text=2,command=lambda n='2':Disp(n), width=2,
height=1)
b.place(relx=0.2, rely=0.4, anchor=CENTER)
b = Button(mygui, text=3,command=lambda n='3':Disp(n), width=2,
height=1)
b.place(relx=0.3, rely=0.4, anchor=CENTER)
b = Button(mygui, text=+,command=lambda n='+':Disp(n), width=2,
height=1)
b.place(relx=0.4, rely=0.4, anchor=CENTER)
b = Button(mygui, text=4,command=lambda n='4':Disp(n), width=2,
height=1)
b.place(relx=0.1, rely=0.5, anchor=CENTER)
b = Button(mygui, text=5,command=lambda n='5':Disp(n), width=2,
height=1)
b.place(relx=0.2, rely=0.5, anchor=CENTER)
b = Button(mygui, text=6,command=lambda n='6':Disp(n), width=2,
height=1)
b.place(relx=0.3, rely=0.5, anchor=CENTER)
b = Button(mygui, text=-,command=lambda n='-':Disp(n), width=2,
height=1)
b.place(relx=0.4, rely=0.5, anchor=CENTER)
b = Button(mygui, text=7,command=lambda n='7':Disp(n), width=2,
height=1)
b.place(relx=0.1, rely=0.6, anchor=CENTER)
b = Button(mygui, text=8,command=lambda n='8':Disp(n), width=2,
height=1)
b.place(relx=0.2, rely=0.6, anchor=CENTER)
b = Button(mygui, text=9,command=lambda n='9':Disp(n), width=2,
height=1)
b.place(relx=0.3, rely=0.6, anchor=CENTER)
b = Button(mygui, text=*,command=lambda n='*':Disp(n), width=2,
height=1)
b.place(relx=0.4, rely=0.6, anchor=CENTER)
b = Button(mygui, text=0,command=lambda n='0':Disp(n), width=2,
height=1)
b.place(relx=0.1, rely=0.7, anchor=CENTER)
b = Button(mygui, text=C,command=Erase, width=2, height=1)
b.place(relx=0.2, rely=0.7, anchor=CENTER)
b = Button(mygui, text=^,command=lambda n='**':Disp(n), width=2,
height=1)
b.place(relx=0.3, rely=0.7, anchor=CENTER)
b = Button(mygui, text=/,command=lambda n='/':Disp(n), width=2,
height=1)
b.place(relx=0.4, rely=0.7, anchor=CENTER)
b = Button(mygui, text=.,command=lambda n='.':Disp(n), width=2,
height=1)
b.place(relx=0.1, rely=0.8, anchor=CENTER)
b = Button(mygui, text=(,command=lambda n='(':Disp(n), width=2,
height=1)
b.place(relx=0.2, rely=0.8, anchor=CENTER)
b = Button(mygui, text=),command=lambda n=')':Disp(n), width=2,
height=1)
b.place(relx=0.3, rely=0.8, anchor=CENTER)
b = Button(mygui, text==,command=Calc, width=2, height=1)
b.place(relx=0.4, rely=0.8, anchor=CENTER)

mygui.mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: questions about Exceptions?

2008-04-10 Thread skanemupp
from __future__ import division
import Tkinter
from Tkinter import *

mygui = Tkinter.Tk()
mygui.title(Calculator)

l = Label(mygui, text=Answer: )
l.place(relx=0.15, rely=0.2, anchor=CENTER)

e = Entry(mygui)
e.place(relx=0.4, rely=0.1, anchor=CENTER)

def Disp(nstr):
e.insert(END, nstr)

def Calc():
expr=e.get()
try:
b = Label(mygui, text=eval(expr))
b.place(relx=0.4, rely=0.2, anchor=CENTER)
except:
b = Label(mygui, text=Not computable)
b.place(relx=0.4, rely=0.2, anchor=CENTER)

def Erase():
e.delete(0,END)


x = 0.1
y = 0.4
for char in '123+456-789*0^./()':
b = Button(mygui, text=char,command=lambda n=char:Disp(n),
width=2, height=1)
b.place(relx=x, rely=y, anchor=CENTER)
x=x+0.1
if x==0.5:
x=0.1
y=y+0.1

b = Button(mygui, text=C,command=Erase, width=2, height=1)
b.place(relx=0.3, rely=0.8, anchor=CENTER)
b = Button(mygui, text==,command=Calc, width=2, height=1)
b.place(relx=0.4, rely=0.8, anchor=CENTER)

mygui.mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


tkinter, overwrite Label-text?

2008-04-10 Thread skanemupp
using python And tkinter.

i have a GUI that outputs a text among other things. after input form
user i want this text to be *)cleared and/or
*)overwritten.

what is the best way to achieve that?

also, how do i make Label-text expand to the right and not to the left?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: questions about Exceptions?

2008-04-10 Thread skanemupp
On 10 Apr, 12:38, [EMAIL PROTECTED] wrote:
  def Calc():
  global nbr
  try:
  print eval(nbr)
  #a = Label(mygui, text=eval(nbr))
  #a.place(relx=0.4, rely=0.1, anchor=CENTER)
  except:
  print Not computable
  nbr = 

  def Erase():
  global nbr
  nbr = 

 Seems to me you could be better off passing a parameter and a return
 statement of None (or your parameter cleaned) for those functions,
 which should work. Given an input, Eval it and then return None. That
 way you wouldn't need the Erase...

i never really got what u meant by this?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tkinter, overwrite Label-text?

2008-04-10 Thread skanemupp
On 10 Apr, 15:28, [EMAIL PROTECTED]
[EMAIL PROTECTED] wrote:
 On Apr 10, 2:35 pm, [EMAIL PROTECTED] wrote:

  using python And tkinter.

  i have a GUI that outputs a text among other things. after input form
  user i want this text to be *)cleared and/or
  *)overwritten.

  what is the best way to achieve that?
  [...]

 Which widget do you use?

 Some widgets can be connected to variables so that when the variable
 changes the widget is automatically update.
 Have a look athttp://docs.python.org/lib/node696.html.

 HTH,
 Dennis Benzinger

i use the Label-widget.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tkinter, overwrite Label-text?

2008-04-10 Thread skanemupp
On 10 Apr, 16:29, [EMAIL PROTECTED]
[EMAIL PROTECTED] wrote:
 On Apr 10, 3:47 pm, [EMAIL PROTECTED] wrote:

  [...]
  i use the Label-widget.

 Then you should be able to connect a variable to your widget like I
 wrote in my previous post.
 Just try out the example from the Python documentation. Of course in
 your case entrythingy would be the
 Label-widget. Take care to assign values to your variable by using the
 set function.

 Dennis Benzinger

i know how to do this already. the problem is i want the text to stay
in the windowa nd not start overwriting Answer:.
i have solved this by using an Entry for the answer as well but id
prefer using a Label.


here is the code:

from __future__ import division
import Tkinter
from Tkinter import *

mygui = Tkinter.Tk()
mygui.title(Calculator)

l = Label(mygui, text=Answer: )
l.place(relx=0.15, rely=0.2, anchor=CENTER)

e = Entry(mygui)
e.place(relx=0.4, rely=0.1, anchor=CENTER)

def Disp(nstr):
e.insert(END, nstr)

def Calc():
expr=e.get()
try:
b = Label(mygui, text=eval(expr))
b.place(relx=0.4, rely=0.2, anchor=CENTER)
except:
b = Label(mygui, text=Not computable)
b.place(relx=0.4, rely=0.2, anchor=CENTER)

def Erase():
e.delete(0,END)


x = 0.1
y = 0.4
for char in '123+456-789*0()/.':
b = Button(mygui, text=char, command=lambda n=char:Disp(n),
width=2, height=1)
b.place(relx=x, rely=y, anchor=CENTER)
x=x+0.1
if x==0.5:
x=0.1
y=y+0.1

b = Button(mygui, text=^, command=lambda n=**:Disp(n), width=2,
height=1)
b.place(relx=0.2, rely=0.8, anchor=CENTER)
b = Button(mygui, text=C,command=Erase, width=2, height=1)
b.place(relx=0.3, rely=0.8, anchor=CENTER)
b = Button(mygui, text==,command=Calc, width=2, height=1)
b.place(relx=0.4, rely=0.8, anchor=CENTER)

mygui.mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


Tkinter: Entry, how to get INSERTpos?

2008-04-10 Thread skanemupp
in this function i want to be able to use the cursor and delete in the
middle of a number.
how do i get the value of anchor or insert?
i want to write:
e.delete(pos-1,pos)

def Backspace():
a=len(e.get())
e.delete(a-1,END)
#e.delete(INSERT, END)
#e.delete(ANCHOR,END)

the complete program:

from __future__ import division
import Tkinter
from Tkinter import *

mygui = Tkinter.Tk()

mygui.title(Calculator)

l = Label(mygui, text=Answer: )
l.place(relx=0.15, rely=0.2, anchor=CENTER)

e = Entry(mygui)
e.place(relx=0.48, rely=0.1, anchor=CENTER, width=173)

c = Entry(mygui)
c.place(relx=0.6, rely=0.2, anchor=CENTER)

def Disp(nstr):
e.insert(INSERT, nstr)

def Calc():
expr=e.get()
c.delete(0, END)
try:
c.insert(END, eval(expr))
except:
c.insert(END, Not computable)

def Erase():
e.delete(0,END)
c.delete(0, END)

def Backspace():
a=len(e.get())
e.delete(a-1,END)
#e.delete(INSERT, END)
#e.delete(ANCHOR,END)


x = 0.1
y = 0.4
for char in '123+456-789*0()/.':
b = Button(mygui, text=char, command=lambda n=char:Disp(n),
width=2, height=1)
b.place(relx=x, rely=y, anchor=CENTER)
x=x+0.1
if x==0.5:
x=0.1
y=y+0.1

b = Button(mygui, text=^, command=lambda n=**:Disp(n), width=2,
height=1)
b.place(relx=0.2, rely=0.8, anchor=CENTER)
b = Button(mygui, text=C,command=Erase, width=2, height=1)
b.place(relx=0.3, rely=0.8, anchor=CENTER)
b = Button(mygui, text=c,command=Backspace, width=2, height=1)
b.place(relx=0.4, rely=0.8, anchor=CENTER)
b = Button(mygui, text==,command=Calc, width=12, height=1)
b.place(relx=0.25, rely=0.9, anchor=CENTER)

mygui.mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


Tkinter, resize window, keep widgets relative placements?

2008-04-10 Thread skanemupp
the code is down below. when i click maximize window it looks terrible
since the widgets are not keeping their relative size.
i guess i could use pack or grid to do that instead of place?
but i tried with pack and grid before and had trouble making it
looking good.

is it possible to have the minimized window just being placed in the
middle without the distance between the buttons and entrys being
enlonge?


from __future__ import division
import Tkinter
from Tkinter import *

mygui = Tkinter.Tk()

mygui.title(Calculator)

l = Label(mygui, text=Answer: )
l.place(relx=0.15, rely=0.2, anchor=CENTER)

e = Entry(mygui)
e.place(relx=0.48, rely=0.1, anchor=CENTER, width=173)

c = Entry(mygui)
c.place(relx=0.6, rely=0.2, anchor=CENTER)

def Disp(nstr):
e.insert(INSERT, nstr)

def Calc():
expr=e.get()
c.delete(0, END)
try:
c.insert(END, eval(expr))
except:
c.insert(END, Not computable)

def Erase():
e.delete(0,END)
c.delete(0, END)

def Backspace():
a=len(e.get())
e.delete(a-1,END)
#e.delete(INSERT, END)
#e.delete(ANCHOR,END)


x = 0.1
y = 0.4
for char in '123+456-789*0()/.':
b = Button(mygui, text=char, command=lambda n=char:Disp(n),
width=2, height=1)
b.place(relx=x, rely=y, anchor=CENTER)
x=x+0.1
if x==0.5:
x=0.1
y=y+0.1

b = Button(mygui, text=^, command=lambda n=**:Disp(n), width=2,
height=1)
b.place(relx=0.2, rely=0.8, anchor=CENTER)
b = Button(mygui, text=C,command=Erase, width=2, height=1)
b.place(relx=0.3, rely=0.8, anchor=CENTER)
b = Button(mygui, text=c,command=Backspace, width=2, height=1)
b.place(relx=0.4, rely=0.8, anchor=CENTER)
b = Button(mygui, text==,command=Calc, width=12, height=1)
b.place(relx=0.25, rely=0.9, anchor=CENTER)

mygui.mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tkinter, resize window, keep widgets relative placements?

2008-04-10 Thread skanemupp
here i didi it with pack() but when i try to use the answerwidget it
gtes all f* up. any suggestions?


from __future__ import division
import Tkinter
from Tkinter import *

mygui = Tkinter.Tk()

mygui.title(Calculator)

##l = Label(mygui, text=Answer: )
##l.grid(row=2, column=1, columnspan=2)

e = Entry(mygui)
e.grid(row=1, column=1, columnspan=4)

c = Entry(mygui)
c.grid(row=2, column=1, columnspan=4)

def Disp(nstr):
e.insert(INSERT, nstr)

def Calc():
expr=e.get()
c.delete(0, END)
try:
c.insert(END, eval(expr))
except:
c.insert(END, Not computable)

def Erase():
e.delete(0,END)
c.delete(0, END)

def Backspace():
a=len(e.get())
e.delete(a-1,END)
#e.delete(INSERT, END)
#e.delete(ANCHOR,END)


x = 1
y = 4
for char in '123+456-789*0()/.':
b = Button(mygui, text=char, command=lambda n=char:Disp(n),
width=2, height=1)
b.grid(row=y, column=x)
x=x+1
if x==5:
x=1
y=y+1

b = Button(mygui, text=^, command=lambda n=**:Disp(n), width=2,
height=1)
b.grid(row=8, column=2)
b = Button(mygui, text=C,command=Erase, width=2, height=1)
b.grid(row=8, column=3)
b = Button(mygui, text=c,command=Backspace, width=2, height=1)
b.grid(row=8, column=4)
b = Button(mygui, text==,command=Calc, width=18, height=1)
b.grid(row=9, column=1, columnspan=8)

mygui.mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


Tkinter, repaint?, keep size?

2008-04-06 Thread skanemupp
so my calculator is almost done for u that have read my previous
posts.
i have some minor problems i have to fix though.

*one is i need to repaint once i have performed a calculation so that
the old results are not left on the screen.
cant find a method for that.

*another is now when i write the expression to be evaluated it resizes
the window as the text grows.
i want the windowsize and all the buttonplacements  stay constant, how
do i achieve this?

-- 
http://mail.python.org/mailman/listinfo/python-list


Prevent GUI layout from changing?

2008-04-06 Thread skanemupp
when i added the results-LABEL the buttons have changed place.
meh why cant i just put buttons and stuff on a specific coordinate and
have them staying there?




#! /usr/bin/env python
from Tkinter import *
import tkMessageBox

class GUIFramework(Frame):
This is the GUI

def __init__(self, master=None):
Initialize yourself

self.expr = 

Initialise the base class
Frame.__init__(self,master)

Set the Window Title
self.master.title(Calculator)

Display the main window
with a little bit of padding
self.grid(padx=10,pady=10)
self.CreateWidgets()



def CreateWidgets(self):

##Create the Entry, set it to be a bit wider
##self.enText = Entry(self)
##self.enText.grid(row=0, column=0, columnspan=3)

Create the Button, set the text and the
command that will be called when the button is clicked
self.btnDisplay = Button(self, text=calculate!,
command=self.Calculate)
self.btnDisplay.grid(row=0, column=31)

Create the Button, set the text and the
command that will be called when the button is clicked
self.lbText = Label(self, text=Results: )
self.lbText.grid(row=1, column=0)


self.btnDisplay = Button(self,text='1',command=lambda
n=1:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=3, column=0, padx=5, pady=5)

self.btnDisplay = Button(self,text='2',command=lambda
n=2:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=3, column=1, padx=5, pady=5)

self.btnDisplay = Button(self,text='3',command=lambda
n=3:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=3, column=2, padx=5, pady=5)

self.btnDisplay = Button(self,text='+',command=lambda
n=+:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=3, column=3, padx=5, pady=5)

self.btnDisplay = Button(self,text='4',command=lambda
n=4:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=4, column=0, padx=5, pady=5)

self.btnDisplay = Button(self,text='5',command=lambda
n=5:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=4, column=1, padx=5, pady=5)

self.btnDisplay = Button(self,text='6',command=lambda
n=6:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=4, column=2, padx=5, pady=5)

self.btnDisplay = Button(self,text='-',command=lambda
n=-:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=4, column=3, padx=5, pady=5)

self.btnDisplay = Button(self,text='7',command=lambda
n=7:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=5, column=0, padx=5, pady=5)

self.btnDisplay = Button(self,text='8',command=lambda
n=8:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=5, column=1, padx=5, pady=5)

self.btnDisplay = Button(self,text='9',command=lambda
n=9:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=5, column=2, padx=5, pady=5)

self.btnDisplay = Button(self,text='*',command=lambda
n=*:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=5, column=3, padx=5, pady=5)

self.btnDisplay = Button(self,text='0',command=lambda
n=0:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=6, column=0, padx=5, pady=5)

self.btnDisplay = Button(self,text='C',command=self.Clean)
self.btnDisplay.grid(row=6, column=1, padx=5, pady=5)

self.btnDisplay = Button(self,text='r',command=lambda
n=r:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=6, column=2, padx=5, pady=5)

self.btnDisplay = Button(self,text='/',command=lambda
n=/:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=6, column=3, padx=5, pady=5)




def Display(self, number):
self.expr = self.expr + number
self.lbText = Label(self, text=self.expr)
self.lbText.grid(row=0, column=0)

def Calculate(self):
self.expr = str(eval(self.expr))#try catch tex 3+6+
self.lbText = Label(self, text=self.expr)
self.lbText.grid(row=1, column=1)
self.expr = 

def Clean(self):
self.expr = 
#self.update()


if __name__ == __main__:
guiFrame = GUIFramework()
guiFrame.mainloop()


-- 
http://mail.python.org/mailman/listinfo/python-list


Tkinter, a lot of buttons, make prog shorter?

2008-04-06 Thread skanemupp
is there anyway to make this shorter? i hate having these big blocks
of similar-looking code, very unaesthetic.
maybe doesnt matter good-code-wise?
anyway can i make some function that makes this shorter?
like put the etiquettes on the button froma string of
'123+456-789*0Cr/' ?
problem is the command and lambda-func for each button is different.


self.btnDisplay = Button(self,text='1',command=lambda
n=1:self.Display(n),width=2,height=2)
self.btnDisplay.grid(row=3, column=0)

self.btnDisplay = Button(self,text='2',command=lambda
n=2:self.Display(n),width=2,height=2)
self.btnDisplay.grid(row=3, column=1)

self.btnDisplay = Button(self,text='3',command=lambda
n=3:self.Display(n),width=2,height=2)
self.btnDisplay.grid(row=3, column=2)

self.btnDisplay = Button(self,text='+',command=lambda
n=+:self.Display(n),width=2,height=2)
self.btnDisplay.grid(row=3, column=3)

self.btnDisplay = Button(self,text='4',command=lambda
n=4:self.Display(n),width=2,height=2)
self.btnDisplay.grid(row=4, column=0)

self.btnDisplay = Button(self,text='5',command=lambda
n=5:self.Display(n),width=2,height=2)
self.btnDisplay.grid(row=4, column=1)

self.btnDisplay = Button(self,text='6',command=lambda
n=6:self.Display(n),width=2,height=2)
self.btnDisplay.grid(row=4, column=2)

self.btnDisplay = Button(self,text='-',command=lambda
n=-:self.Display(n),width=2,height=2)
self.btnDisplay.grid(row=4, column=3)

self.btnDisplay = Button(self,text='7',command=lambda
n=7:self.Display(n),width=2,height=2)
self.btnDisplay.grid(row=5, column=0)

self.btnDisplay = Button(self,text='8',command=lambda
n=8:self.Display(n),width=2,height=2)
self.btnDisplay.grid(row=5, column=1)

self.btnDisplay = Button(self,text='9',command=lambda
n=9:self.Display(n),width=2,height=2)
self.btnDisplay.grid(row=5, column=2)

self.btnDisplay = Button(self,text='*',command=lambda
n=*:self.Display(n),width=2,height=2)
self.btnDisplay.grid(row=5, column=3)

self.btnDisplay = Button(self,text='0',command=lambda
n=0:self.Display(n),width=2,height=2)
self.btnDisplay.grid(row=6, column=0)

self.btnDisplay =
Button(self,text='C',command=self.Clean,width=2,height=2)
self.btnDisplay.grid(row=6, column=1)

self.btnDisplay = Button(self,text='r',command=lambda
n=r:self.Display(n),width=2,height=2)
self.btnDisplay.grid(row=6, column=2)

self.btnDisplay = Button(self,text='/',command=lambda
n=/:self.Display(n),width=2,height=2)
self.btnDisplay.grid(row=6, column=3)
-- 
http://mail.python.org/mailman/listinfo/python-list


How to create an exe-file?

2008-04-06 Thread skanemupp
how do you create exe-files of your python-code?

is it different depending on what libraries, GUI-frameworks you use?

i want to create an exe-file of a pythonscript that uses Tkinter.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to create an exe-file?

2008-04-06 Thread skanemupp
On 6 Apr, 22:50, Fredrik Lundh [EMAIL PROTECTED] wrote:
 [EMAIL PROTECTED] wrote:
  how do you create exe-files of your python-code?

  is it different depending on what libraries, GUI-frameworks you use?

  i want to create an exe-file of a pythonscript that uses Tkinter.

 assuming windows only, you want:

 http://www.py2exe.org/

 also see:

 http://effbot.org/zone/python-compile.htm

 /F


ty.

a good thing about python is the portability though. but u cant make
an exe that can be used on mac too, ie one exe fpr both?

if i want to make an exe for mac, what do i need?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Prevent GUI layout from changing?

2008-04-06 Thread skanemupp
On 6 Apr, 22:15, Gabriel Genellina [EMAIL PROTECTED] wrote:
 En Sun, 06 Apr 2008 15:12:55 -0300, [EMAIL PROTECTED] escribió:

 I can't help with your sizing problem, I don't know grids. But don't do
 this:

  def Display(self, number):
  self.expr = self.expr + number
  self.lbText = Label(self, text=self.expr)
  self.lbText.grid(row=0, column=0)

  def Calculate(self):
  self.expr = str(eval(self.expr))#try catch tex 3+6+
  self.lbText = Label(self, text=self.expr)
  self.lbText.grid(row=1, column=1)
  self.expr = 

 You're creating a *new* Label object for each keystroke (they stack on the
 same place and only the newest is visible, I presume).
 Instead, you should change the text inside the existing widget:

  self.lbText.config(text=self.expr)

 (there is no need to reposition the widget)
 Label is described herehttp://effbot.org/tkinterbook/label.htm
 and you may want to learn to use Tkinter variables:  
 http://effbot.org/tkinterbook/variable.htm

 --
 Gabriel Genellina


the problem is that when i start writing long numbers the window grows
bigger which is really annoying. is that ebcause of what u said?

as of now i can see exactly the expression i typed and the answer it
is just annoing that it doesnt stay the same size.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tkinter, a lot of buttons, make prog shorter?

2008-04-06 Thread skanemupp
should i not use self. in Clean() and Calculate() either?

anyway i had changed it before. must have copied the wrong program.



#! /usr/bin/env python
from Tkinter import *
import tkMessageBox

class GUIFramework(Frame):
This is the GUI

def __init__(self, master=None):
Initialize yourself

self.expr = 

Initialise the base class
Frame.__init__(self,master)

Set the Window Title
self.master.title(Calculator)

Display the main window
with a little bit of padding
self.grid(padx=10,pady=10)
self.CreateWidgets()



def CreateWidgets(self):

Create the Button, set the text and the
command that will be called when the button is clicked
btnDisplay = Button(self, text=calculate!,
command=self.Calculate)
btnDisplay.grid(row=0, column=31)

##Create the Button, set the text and the
##command that will be called when the button is clicked
##self.lbText = Label(self, text=Results:)
##self.lbText.grid(row=1, column=0)


btnDisplay = Button(self,text='1',command=lambda
n=1:self.Display(n),width=2,height=2)
btnDisplay.grid(row=3, column=0)

btnDisplay = Button(self,text='2',command=lambda
n=2:self.Display(n),width=2,height=2)
btnDisplay.grid(row=3, column=1)

btnDisplay = Button(self,text='3',command=lambda
n=3:self.Display(n),width=2,height=2)
btnDisplay.grid(row=3, column=2)

btnDisplay = Button(self,text='+',command=lambda
n=+:self.Display(n),width=2,height=2)
btnDisplay.grid(row=3, column=3)

btnDisplay = Button(self,text='4',command=lambda
n=4:self.Display(n),width=2,height=2)
btnDisplay.grid(row=4, column=0)

btnDisplay = Button(self,text='5',command=lambda
n=5:self.Display(n),width=2,height=2)
btnDisplay.grid(row=4, column=1)

btnDisplay = Button(self,text='6',command=lambda
n=6:self.Display(n),width=2,height=2)
btnDisplay.grid(row=4, column=2)

btnDisplay = Button(self,text='-',command=lambda
n=-:self.Display(n),width=2,height=2)
btnDisplay.grid(row=4, column=3)

btnDisplay = Button(self,text='7',command=lambda
n=7:self.Display(n),width=2,height=2)
btnDisplay.grid(row=5, column=0)

btnDisplay = Button(self,text='8',command=lambda
n=8:self.Display(n),width=2,height=2)
btnDisplay.grid(row=5, column=1)

btnDisplay = Button(self,text='9',command=lambda
n=9:self.Display(n),width=2,height=2)
btnDisplay.grid(row=5, column=2)

btnDisplay = Button(self,text='*',command=lambda
n=*:self.Display(n),width=2,height=2)
btnDisplay.grid(row=5, column=3)

btnDisplay = Button(self,text='0',command=lambda
n=0:self.Display(n),width=2,height=2)
btnDisplay.grid(row=6, column=0)

btnDisplay =
Button(self,text='C',command=self.Clean,width=2,height=2)
btnDisplay.grid(row=6, column=1)

btnDisplay = Button(self,text='^.5',command=lambda n=**.
5:self.Display(n),width=2,height=2)
btnDisplay.grid(row=6, column=2)

btnDisplay = Button(self,text='/',command=lambda
n=/:self.Display(n),width=2,height=2)
btnDisplay.grid(row=6, column=3)




def Display(self, number):
self.expr = self.expr + number
self.lbText = Label(self, text=self.expr)
self.lbText.grid(row=0, column=0)

def Calculate(self):
self.expr = str(eval(self.expr))#try catch tex 3+6+
result = self.expr
self.Clean()
self.lbText = Label(self, text=result)
self.lbText.grid(row=0, column=0)
self.expr = 

def Clean(self):
self.expr =  
self.lbText.config(text=self.expr)
self.lbText = Label(self, text=self.expr)
self.lbText.grid(row=0, column=0)
self.expr = 
self.lbText.config(text=self.expr)
self.lbText = Label(self, text=self.expr)
self.lbText.grid(row=0, column=0)



if __name__ == __main__:
guiFrame = GUIFramework()
guiFrame.mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TKinter, buttonwidget response problem(1) and all btns the same size(2)!

2008-04-05 Thread skanemupp
am i not doing that then? did u look at the code? where do i add the
bind if this:
btnDisplay = Button(self, text=1, command=self.Display)
btnDisplay.grid(row=3, column=0, padx=5, pady=5)

is not enough?



def Display(self, event_obj):
button = event_obj.widget
text = button.cget(text)

if text==1:
print 1



i get this exception so i need to pass another argument? im passing 2
no?

Exception in Tkinter callback
Traceback (most recent call last):
  File C:\Python25\lib\lib-tk\Tkinter.py, line 1403, in __call__
return self.func(*args)
TypeError: Display() takes exactly 2 arguments (1 given)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TKinter, buttonwidget response problem(1) and all btns the same size(2)!

2008-04-05 Thread skanemupp


 def __init__(self):
 # ...
 button = Button(self,
 text='1',
 command=lambda n=1: self.display(n))
 # ...

 def display(self, number):
 print number



should this really be inside the __init__ function?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TKinter, buttonwidget response problem(1) and all btns the same size(2)!

2008-04-05 Thread skanemupp
On 5 Apr, 17:09, Fredrik Lundh [EMAIL PROTECTED] wrote:
 [EMAIL PROTECTED] wrote:
  def __init__(self):
  # ...
  button = Button(self,
  text='1',
  command=lambda n=1: self.display(n))
  # ...

  def display(self, number):
  print number

  should this really be inside the __init__ function?

 what's wrong with creating widgets in an __init__ method?

 /F


i dont know, i used a piece of code i found which had a createwidgets-
method. isnt init a function, not a method btw(or it is the same
thing?)




this is the current code, only included 2 buttons here to make it
shorter. i might still have misinterpreted though, is the code
correct(it runs correct at least...)?
whats the advantage/disadvante to have it inside the init?


#! /usr/bin/env python
from Tkinter import *
import tkMessageBox

class GUIFramework(Frame):
This is the GUI

def __init__(self, master=None):
Initialize yourself

Initialise the base class
Frame.__init__(self,master)

Set the Window Title
self.master.title(Calculator)

Display the main window
with a little bit of padding
self.grid(padx=10,pady=10)
self.CreateWidgets()



def CreateWidgets(self):


self.btnDisplay = Button(self,text='1',command=lambda
n=1:self.Display(n))
self.btnDisplay.grid(row=3, column=0, padx=5, pady=5)

self.btnDisplay = Button(self,text='/',command=lambda
n=/:self.Display(n))
self.btnDisplay.grid(row=6, column=3, padx=5, pady=5)

def Display(self, number):
print number

if __name__ == __main__:
guiFrame = GUIFramework()
guiFrame.mainloop()



-- 
http://mail.python.org/mailman/listinfo/python-list


Tkinter: making buttons the same size?

2008-04-05 Thread skanemupp
on windows vista these buttons dont have the same size, the /
shrinks a little. how do i make them the same size or prevent
shrinking?
a mac-user told me they look the same to him so maybe it doesnt shrink
on macs but it does when using VISTA.

i tried some .propagate and extend-stuff but didnt work.

#! /usr/bin/env python
from Tkinter import *
import tkMessageBox

class GUIFramework(Frame):
This is the GUI

def __init__(self, master=None):
Initialize yourself

Initialise the base class
Frame.__init__(self,master)

Set the Window Title
self.master.title(Calculator)

Display the main window
with a little bit of padding
self.grid(padx=10,pady=10)
self.CreateWidgets()



def CreateWidgets(self):


self.btnDisplay = Button(self,text='1',command=lambda
n=1:self.Display(n))
self.btnDisplay.grid(row=3, column=0, padx=5, pady=5)

self.btnDisplay = Button(self,text='/',command=lambda
n=/:self.Display(n))
self.btnDisplay.grid(row=6, column=3, padx=5, pady=5)

def Display(self, number):
print number

if __name__ == __main__:
guiFrame = GUIFramework()
guiFrame.mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tkinter: making buttons the same size?

2008-04-05 Thread skanemupp
how do i do that? i get this error:


self.btnDisplay = Button(self,text='1',command=lambda
n=1:self.Display(n))
self.btnDisplay.grid(row=3, column=0, padx=5, pady=5, width=1)

self.btnDisplay = Button(self,text='/',command=lambda
n=/:self.Display(n))
self.btnDisplay.grid(row=6, column=3, padx=5, pady=5, width=1)

Traceback (most recent call last):
  File C:\Users\saftarn\Desktop\guiexperiments
\calcguiworkingshort.py, line 37, in module
guiFrame = GUIFramework()
  File C:\Users\saftarn\Desktop\guiexperiments
\calcguiworkingshort.py, line 20, in __init__
self.CreateWidgets()
  File C:\Users\saftarn\Desktop\guiexperiments
\calcguiworkingshort.py, line 28, in CreateWidgets
self.btnDisplay.grid(row=3, column=0, padx=5, pady=5, width=1)
  File C:\Python25\lib\lib-tk\Tkinter.py, line 1859, in
grid_configure
+ self._options(cnf, kw))
TclError: bad option -width: must be -column, -columnspan, -in, -
ipadx, -ipady, -padx, -pady, -row, -rowspan, or -sticky
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TKinter, buttonwidget response problem(1) and all btns the same size(2)!

2008-04-05 Thread skanemupp
it works now. here is all the code:

#! /usr/bin/env python
from Tkinter import *
import tkMessageBox

class GUIFramework(Frame):
This is the GUI

def __init__(self, master=None):
Initialize yourself

Initialise the base class
Frame.__init__(self,master)

Set the Window Title
self.master.title(Calculator)

Display the main window
with a little bit of padding
self.grid(padx=10,pady=10)
self.CreateWidgets()



def CreateWidgets(self):

##Create the Entry, set it to be a bit wider
##self.enText = Entry(self)
##self.enText.grid(row=0, column=0, columnspan=3)

Create the Button, set the text and the
command that will be called when the button is clicked
self.btnDisplay = Button(self, text=calculate!,
state=DISABLED)
self.btnDisplay.grid(row=0, column=31)


self.btnDisplay = Button(self,text='1',command=lambda
n=1:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=3, column=0, padx=5, pady=5)

self.btnDisplay = Button(self,text='2',command=lambda
n=2:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=3, column=1, padx=5, pady=5)

self.btnDisplay = Button(self,text='3',command=lambda
n=3:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=3, column=2, padx=5, pady=5)

self.btnDisplay = Button(self,text='+',command=lambda
n=+:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=3, column=3, padx=5, pady=5)

self.btnDisplay = Button(self,text='4',command=lambda
n=4:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=4, column=0, padx=5, pady=5)

self.btnDisplay = Button(self,text='5',command=lambda
n=5:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=4, column=1, padx=5, pady=5)

self.btnDisplay = Button(self,text='6',command=lambda
n=6:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=4, column=2, padx=5, pady=5)

self.btnDisplay = Button(self,text='-',command=lambda
n=-:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=4, column=3, padx=5, pady=5)

self.btnDisplay = Button(self,text='7',command=lambda
n=7:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=5, column=0, padx=5, pady=5)

self.btnDisplay = Button(self,text='8',command=lambda
n=8:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=5, column=1, padx=5, pady=5)

self.btnDisplay = Button(self,text='9',command=lambda
n=9:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=5, column=2, padx=5, pady=5)

self.btnDisplay = Button(self,text='*',command=lambda
n=*:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=5, column=3, padx=5, pady=5)

self.btnDisplay = Button(self,text='0',command=lambda
n=0:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=6, column=0, padx=5, pady=5)

self.btnDisplay = Button(self,text='C',command=lambda
n=C:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=6, column=1, padx=5, pady=5)

self.btnDisplay = Button(self,text='r',command=lambda
n=r:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=6, column=2, padx=5, pady=5)

self.btnDisplay = Button(self,text='/',command=lambda
n=/:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=6, column=3, padx=5, pady=5)




def Display(self, number):
#print number
self.lbText = Label(self, text=number)
self.lbText.grid(row=0, column=0)



if __name__ == __main__:
guiFrame = GUIFramework()
guiFrame.mainloop()

-- 
http://mail.python.org/mailman/listinfo/python-list


Tkinter, add pressed buttons onto string display string, how to?

2008-04-05 Thread skanemupp
using tkinter and python i now have a small App (that will hopefully
soon be a fully functioning calculator) where u can push buttons and
the corresponding number or operator is shown.

when u press 1, 1 appears on the screen, pres + and + appears etc.

at the moment every output overwrites the previous so what i want to
do is obviosuly to add every new output to a string so that i can read
the string and perform the calculation.

so i want:
*  when pushing the button push the token of the button onto a string
* display the new string, ie 1+2 for example
* want to be able to access this string when pressing calculate so i
can figure out which operators should
be done first so it can solve something like this: (1+2*3)(3-4/2)
and not just simple 1+2-stuff.

do i have to have some global string-variable in the GUIframework
then? im not sure where to start...


here is the code:

#! /usr/bin/env python
from Tkinter import *
import tkMessageBox

class GUIFramework(Frame):
This is the GUI

def __init__(self, master=None):
Initialize yourself

Initialise the base class
Frame.__init__(self,master)

Set the Window Title
self.master.title(Calculator)

Display the main window
with a little bit of padding
self.grid(padx=10,pady=10)
self.CreateWidgets()



def CreateWidgets(self):

##Create the Entry, set it to be a bit wider
##self.enText = Entry(self)
##self.enText.grid(row=0, column=0, columnspan=3)

Create the Button, set the text and the
command that will be called when the button is clicked
self.btnDisplay = Button(self, text=calculate!,
state=DISABLED)
self.btnDisplay.grid(row=0, column=31)


self.btnDisplay = Button(self,text='1',command=lambda
n=1:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=3, column=0, padx=5, pady=5)

self.btnDisplay = Button(self,text='2',command=lambda
n=2:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=3, column=1, padx=5, pady=5)

self.btnDisplay = Button(self,text='3',command=lambda
n=3:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=3, column=2, padx=5, pady=5)

self.btnDisplay = Button(self,text='+',command=lambda
n=+:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=3, column=3, padx=5, pady=5)

self.btnDisplay = Button(self,text='4',command=lambda
n=4:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=4, column=0, padx=5, pady=5)

self.btnDisplay = Button(self,text='5',command=lambda
n=5:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=4, column=1, padx=5, pady=5)

self.btnDisplay = Button(self,text='6',command=lambda
n=6:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=4, column=2, padx=5, pady=5)

self.btnDisplay = Button(self,text='-',command=lambda
n=-:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=4, column=3, padx=5, pady=5)

self.btnDisplay = Button(self,text='7',command=lambda
n=7:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=5, column=0, padx=5, pady=5)

self.btnDisplay = Button(self,text='8',command=lambda
n=8:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=5, column=1, padx=5, pady=5)

self.btnDisplay = Button(self,text='9',command=lambda
n=9:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=5, column=2, padx=5, pady=5)

self.btnDisplay = Button(self,text='*',command=lambda
n=*:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=5, column=3, padx=5, pady=5)

self.btnDisplay = Button(self,text='0',command=lambda
n=0:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=6, column=0, padx=5, pady=5)

self.btnDisplay = Button(self,text='C',command=lambda
n=C:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=6, column=1, padx=5, pady=5)

self.btnDisplay = Button(self,text='r',command=lambda
n=r:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=6, column=2, padx=5, pady=5)

self.btnDisplay = Button(self,text='/',command=lambda
n=/:self.Display(n),width=1,height=1)
self.btnDisplay.grid(row=6, column=3, padx=5, pady=5)




def Display(self, number):
#print number
self.lbText = Label(self, text=number)
self.lbText.grid(row=0, column=0)



if __name__ == __main__:
guiFrame = GUIFramework()
guiFrame.mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tkinter, add pressed buttons onto string display string, how to?

2008-04-05 Thread skanemupp

 input = hello
 input +=  world
 print input

this i know. im wondering how to handle the variable in the actual
program. this exception i get:
Exception in Tkinter callback
Traceback (most recent call last):
  File C:\Python25\lib\lib-tk\Tkinter.py, line 1403, in __call__
return self.func(*args)
  File C:\Users\saftarn\Desktop\guiexperiments\calculatorGUI.py,
line 48, in lambda
self.btnDisplay = Button(self,text='+',command=lambda
n=+:self.Display(n),width=1,height=1)
  File C:\Users\saftarn\Desktop\guiexperiments\calculatorGUI.py,
line 92, in Display
str = number + str
UnboundLocalError: local variable 'str' referenced before assignment


 A caculator program is pretty complex.  Based on your rudimentary
 questions, I don't think you have enough programming experience to
 tackle a project like that yet.

nah ill be fine. im new to python, not to programming.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tkinter, add pressed buttons onto string display string, how to?

2008-04-05 Thread skanemupp
 Just like the message says: You are trying to use `str` (on the right hand
 side of the assignment) before anything is bound to that name.

 Ciao,
 Marc 'BlackJack' Rintsch


i know but i want the variable str(which i found out is a reserved
word so i changed it) to be accessible all over __init__ right?
so i tried to delcare it in __init__ in the beginning of the framework
class but then when i access it in the method Display i get that
error.

so how should i declare this variable to be able to access it
everywhere?

i want another method calculate that can access the same string
later and do the calculations(writing some other code now that will
read and interpret that).
-- 
http://mail.python.org/mailman/listinfo/python-list


Best way to check if string is an integer?

2008-04-05 Thread skanemupp
which is the best way to check if a string is an number or a char?
could the 2nd example be very expensive timewise if i have to check a
lot of strings?

this

value = raw_input()

try:
value = int(value)
except ValueError:
print value is not an integer


or:


c=raw_input(yo: )
if c in '0123456789':
print integer
else:
print char



or some other way?
-- 
http://mail.python.org/mailman/listinfo/python-list


while-loops enter the last time after condition is filled?

2008-04-05 Thread skanemupp
it seems to me from my results that when i use a while-loop it will
execute once after the condition is met.

ie the conditions is met the code executes one time more and then
quits.
-- 
http://mail.python.org/mailman/listinfo/python-list


TKinter, buttonwidget response problem(1) and all btns the same size(2)!

2008-04-04 Thread skanemupp
1st question:

when i run this program 1 will be printed into the interpreter when i
run it BUT without me clicking the actual button.
when i then click the button 1, nothing happens.

obv i dont want any output when i dont push the button but i want it
when i do.

what am i doing wrong here?



2nd question:

i want all the buttons to have the same size. i thought i should use
row/columnspan but i dont get that to work.
how should i do?



[code]
#! /usr/bin/env python
from Tkinter import *
import tkMessageBox

class GUIFramework(Frame):
This is the GUI

def __init__(self,master=None):
Initialize yourself

Initialise the base class
Frame.__init__(self,master)

Set the Window Title
self.master.title(Calculator)

Display the main window
with a little bit of padding
self.grid(padx=10,pady=10)
self.CreateWidgets()

def CreateWidgets(self):

self.enText = Entry(self)
self.enText.grid(row=0, column=0, columnspan=8, padx=5,
pady=5)

self.enText = Entry(self)
self.enText.grid(row=1, column=0, columnspan=8, padx=5,
pady=5)

self.btnDisplay = Button(self, text=1,
command=self.Display(1))
self.btnDisplay.grid(row=3, column=0, padx=5, pady=5)

self.btnDisplay = Button(self, text=2, default=ACTIVE)
self.btnDisplay.grid(row=3, column=1, padx=5, pady=5)

self.btnDisplay = Button(self, text=3, default=ACTIVE)
self.btnDisplay.grid(row=3, column=2, padx=5, pady=5)

self.btnDisplay = Button(self, text=+, default=ACTIVE)
self.btnDisplay.grid(row=3, column=3, padx=5, pady=5)

self.btnDisplay = Button(self, text=4, default=ACTIVE)
self.btnDisplay.grid(row=4, column=0, padx=5, pady=5)

self.btnDisplay = Button(self, text=5, default=ACTIVE)
self.btnDisplay.grid(row=4, column=1, padx=5, pady=5)

self.btnDisplay = Button(self, text=6, default=ACTIVE)
self.btnDisplay.grid(row=4, column=2, padx=5, pady=5)

self.btnDisplay = Button(self, text=-, default=ACTIVE)
self.btnDisplay.grid(row=4, column=3, padx=5, pady=5)

self.btnDisplay = Button(self, text=7, default=ACTIVE)
self.btnDisplay.grid(row=5, column=0, padx=5, pady=5)

self.btnDisplay = Button(self, text=8, default=ACTIVE)
self.btnDisplay.grid(row=5, column=1, padx=5, pady=5)

self.btnDisplay = Button(self, text=9, default=ACTIVE)
self.btnDisplay.grid(row=5, column=2, padx=5, pady=5)

self.btnDisplay = Button(self, text=*, default=ACTIVE)
self.btnDisplay.grid(row=5, column=3, padx=5, pady=5)

self.btnDisplay = Button(self, text=0, default=ACTIVE)
self.btnDisplay.grid(row=6, column=0, padx=5, pady=5)

self.btnDisplay = Button(self, text=C, default=ACTIVE)
self.btnDisplay.grid(row=6, column=1, padx=5, pady=5)

self.btnDisplay = Button(self, text=r, default=ACTIVE)
self.btnDisplay.grid(row=6, column=2, padx=5, pady=5)

self.btnDisplay = Button(self, text=/, default=ACTIVE)
self.btnDisplay.grid(row=6, column=3, padx=5, pady=5)

def Display(self, xbtn):
if xbtn==1:
print 1

if __name__ == __main__:
guiFrame = GUIFramework()
guiFrame.mainloop()

[/code]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TKinter, buttonwidget response problem(1) and all btns the same size(2)!

2008-04-04 Thread skanemupp
On 5 Apr, 05:26, 7stud [EMAIL PROTECTED] wrote:
 On Apr 4, 7:06 pm, [EMAIL PROTECTED] wrote:



  1st question:

  when i run this program 1 will be printed into the interpreter when i
  run it BUT without me clicking the actual button.
  when i then click the button 1, nothing happens.

  obv i dont want any output when i dont push the button but i want it
  when i do.

  what am i doing wrong here?

  2nd question:

  i want all the buttons to have the same size. i thought i should use
  row/columnspan but i dont get that to work.
  how should i do?

  [code]
  #! /usr/bin/env python
  from Tkinter import *
  import tkMessageBox

  class GUIFramework(Frame):
  This is the GUI

  def __init__(self,master=None):
  Initialize yourself

  Initialise the base class
  Frame.__init__(self,master)

  Set the Window Title
  self.master.title(Calculator)

  Display the main window
  with a little bit of padding
  self.grid(padx=10,pady=10)
  self.CreateWidgets()

  def CreateWidgets(self):

  self.enText = Entry(self)
  self.enText.grid(row=0, column=0, columnspan=8, padx=5,
  pady=5)

  self.enText = Entry(self)
  self.enText.grid(row=1, column=0, columnspan=8, padx=5,
  pady=5)

  self.btnDisplay = Button(self, text=1,
  command=self.Display(1))
  self.btnDisplay.grid(row=3, column=0, padx=5, pady=5)

  self.btnDisplay = Button(self, text=2, default=ACTIVE)
  self.btnDisplay.grid(row=3, column=1, padx=5, pady=5)

  self.btnDisplay = Button(self, text=3, default=ACTIVE)
  self.btnDisplay.grid(row=3, column=2, padx=5, pady=5)

  self.btnDisplay = Button(self, text=+, default=ACTIVE)
  self.btnDisplay.grid(row=3, column=3, padx=5, pady=5)

  self.btnDisplay = Button(self, text=4, default=ACTIVE)
  self.btnDisplay.grid(row=4, column=0, padx=5, pady=5)

  self.btnDisplay = Button(self, text=5, default=ACTIVE)
  self.btnDisplay.grid(row=4, column=1, padx=5, pady=5)

  self.btnDisplay = Button(self, text=6, default=ACTIVE)
  self.btnDisplay.grid(row=4, column=2, padx=5, pady=5)

  self.btnDisplay = Button(self, text=-, default=ACTIVE)
  self.btnDisplay.grid(row=4, column=3, padx=5, pady=5)

  self.btnDisplay = Button(self, text=7, default=ACTIVE)
  self.btnDisplay.grid(row=5, column=0, padx=5, pady=5)

  self.btnDisplay = Button(self, text=8, default=ACTIVE)
  self.btnDisplay.grid(row=5, column=1, padx=5, pady=5)

  self.btnDisplay = Button(self, text=9, default=ACTIVE)
  self.btnDisplay.grid(row=5, column=2, padx=5, pady=5)

  self.btnDisplay = Button(self, text=*, default=ACTIVE)
  self.btnDisplay.grid(row=5, column=3, padx=5, pady=5)

  self.btnDisplay = Button(self, text=0, default=ACTIVE)
  self.btnDisplay.grid(row=6, column=0, padx=5, pady=5)

  self.btnDisplay = Button(self, text=C, default=ACTIVE)
  self.btnDisplay.grid(row=6, column=1, padx=5, pady=5)

  self.btnDisplay = Button(self, text=r, default=ACTIVE)
  self.btnDisplay.grid(row=6, column=2, padx=5, pady=5)

  self.btnDisplay = Button(self, text=/, default=ACTIVE)
  self.btnDisplay.grid(row=6, column=3, padx=5, pady=5)

  def Display(self, xbtn):
  if xbtn==1:
  print 1

  if __name__ == __main__:
  guiFrame = GUIFramework()
  guiFrame.mainloop()

  [/code]

 If you have this function:

 def f():
print 1
return 10

 and you write:

 result = f()

 The '()' is the function execution operator; it tells python to
 execute the function.  In this case, the function executes, and then
 the return value of the function is assigned to the variable result.
 If a function does not have a return statement, then the function
 returns None by default.

 The same thing is happening in this portion of your code:

 command = self.Display(1)

 That code tells python to execute the Display function and assign the
 function's return value to the variable command.  As a result Display
 executes and 1 is displayed.  Then since Dispay does not have a return
 statement, None is returned, and None is assigned to command.
 Obviously, that is not what you want to do.

 What you want to do is assign a function reference to command so
 that python can execute the function sometime later when you click on
 the button.  A function reference is just the function name without
 the '()' after it.  So you would write:

 command = self.Display

 But writing it like that doesn't allow *you* to pass any arguments to
 Display().  In addition, *tkinter* does not pass any arguments to
 Display when tkinter calls Display in response to a button click.  As
 a result, there is no way to pass an argument to Display.

 However, there is another way to cause a function to execute when an
 

Re: TKinter, buttonwidget response problem(1) and all btns the same size(2)!

2008-04-04 Thread skanemupp
On 5 Apr, 05:57, [EMAIL PROTECTED] wrote:
 On 5 Apr, 05:26, 7stud [EMAIL PROTECTED] wrote:



  On Apr 4, 7:06 pm, [EMAIL PROTECTED] wrote:

   1st question:

   when i run this program 1 will be printed into the interpreter when i
   run it BUT without me clicking the actual button.
   when i then click the button 1, nothing happens.

   obv i dont want any output when i dont push the button but i want it
   when i do.

   what am i doing wrong here?

   2nd question:

   i want all the buttons to have the same size. i thought i should use
   row/columnspan but i dont get that to work.
   how should i do?

   [code]
   #! /usr/bin/env python
   from Tkinter import *
   import tkMessageBox

   class GUIFramework(Frame):
   This is the GUI

   def __init__(self,master=None):
   Initialize yourself

   Initialise the base class
   Frame.__init__(self,master)

   Set the Window Title
   self.master.title(Calculator)

   Display the main window
   with a little bit of padding
   self.grid(padx=10,pady=10)
   self.CreateWidgets()

   def CreateWidgets(self):

   self.enText = Entry(self)
   self.enText.grid(row=0, column=0, columnspan=8, padx=5,
   pady=5)

   self.enText = Entry(self)
   self.enText.grid(row=1, column=0, columnspan=8, padx=5,
   pady=5)

   self.btnDisplay = Button(self, text=1,
   command=self.Display(1))
   self.btnDisplay.grid(row=3, column=0, padx=5, pady=5)

   self.btnDisplay = Button(self, text=2, default=ACTIVE)
   self.btnDisplay.grid(row=3, column=1, padx=5, pady=5)

   self.btnDisplay = Button(self, text=3, default=ACTIVE)
   self.btnDisplay.grid(row=3, column=2, padx=5, pady=5)

   self.btnDisplay = Button(self, text=+, default=ACTIVE)
   self.btnDisplay.grid(row=3, column=3, padx=5, pady=5)

   self.btnDisplay = Button(self, text=4, default=ACTIVE)
   self.btnDisplay.grid(row=4, column=0, padx=5, pady=5)

   self.btnDisplay = Button(self, text=5, default=ACTIVE)
   self.btnDisplay.grid(row=4, column=1, padx=5, pady=5)

   self.btnDisplay = Button(self, text=6, default=ACTIVE)
   self.btnDisplay.grid(row=4, column=2, padx=5, pady=5)

   self.btnDisplay = Button(self, text=-, default=ACTIVE)
   self.btnDisplay.grid(row=4, column=3, padx=5, pady=5)

   self.btnDisplay = Button(self, text=7, default=ACTIVE)
   self.btnDisplay.grid(row=5, column=0, padx=5, pady=5)

   self.btnDisplay = Button(self, text=8, default=ACTIVE)
   self.btnDisplay.grid(row=5, column=1, padx=5, pady=5)

   self.btnDisplay = Button(self, text=9, default=ACTIVE)
   self.btnDisplay.grid(row=5, column=2, padx=5, pady=5)

   self.btnDisplay = Button(self, text=*, default=ACTIVE)
   self.btnDisplay.grid(row=5, column=3, padx=5, pady=5)

   self.btnDisplay = Button(self, text=0, default=ACTIVE)
   self.btnDisplay.grid(row=6, column=0, padx=5, pady=5)

   self.btnDisplay = Button(self, text=C, default=ACTIVE)
   self.btnDisplay.grid(row=6, column=1, padx=5, pady=5)

   self.btnDisplay = Button(self, text=r, default=ACTIVE)
   self.btnDisplay.grid(row=6, column=2, padx=5, pady=5)

   self.btnDisplay = Button(self, text=/, default=ACTIVE)
   self.btnDisplay.grid(row=6, column=3, padx=5, pady=5)

   def Display(self, xbtn):
   if xbtn==1:
   print 1

   if __name__ == __main__:
   guiFrame = GUIFramework()
   guiFrame.mainloop()

   [/code]

  If you have this function:

  def f():
 print 1
 return 10

  and you write:

  result = f()

  The '()' is the function execution operator; it tells python to
  execute the function.  In this case, the function executes, and then
  the return value of the function is assigned to the variable result.
  If a function does not have a return statement, then the function
  returns None by default.

  The same thing is happening in this portion of your code:

  command = self.Display(1)

  That code tells python to execute the Display function and assign the
  function's return value to the variable command.  As a result Display
  executes and 1 is displayed.  Then since Dispay does not have a return
  statement, None is returned, and None is assigned to command.
  Obviously, that is not what you want to do.

  What you want to do is assign a function reference to command so
  that python can execute the function sometime later when you click on
  the button.  A function reference is just the function name without
  the '()' after it.  So you would write:

  command = self.Display

  But writing it like that doesn't allow *you* to pass any arguments to
  Display().  In addition, *tkinter* does not pass any arguments to
  Display when tkinter calls Display in response to a button 

Re: TKinter, buttonwidget response problem(1) and all btns the same size(2)!

2008-04-04 Thread skanemupp
On 5 Apr, 07:02, [EMAIL PROTECTED] wrote:
 On 5 Apr, 05:57, [EMAIL PROTECTED] wrote:



  On 5 Apr, 05:26, 7stud [EMAIL PROTECTED] wrote:

   On Apr 4, 7:06 pm, [EMAIL PROTECTED] wrote:

1st question:

when i run this program 1 will be printed into the interpreter when i
run it BUT without me clicking the actual button.
when i then click the button 1, nothing happens.

obv i dont want any output when i dont push the button but i want it
when i do.

what am i doing wrong here?

2nd question:

i want all the buttons to have the same size. i thought i should use
row/columnspan but i dont get that to work.
how should i do?

[code]
#! /usr/bin/env python
from Tkinter import *
import tkMessageBox

class GUIFramework(Frame):
This is the GUI

def __init__(self,master=None):
Initialize yourself

Initialise the base class
Frame.__init__(self,master)

Set the Window Title
self.master.title(Calculator)

Display the main window
with a little bit of padding
self.grid(padx=10,pady=10)
self.CreateWidgets()

def CreateWidgets(self):

self.enText = Entry(self)
self.enText.grid(row=0, column=0, columnspan=8, padx=5,
pady=5)

self.enText = Entry(self)
self.enText.grid(row=1, column=0, columnspan=8, padx=5,
pady=5)

self.btnDisplay = Button(self, text=1,
command=self.Display(1))
self.btnDisplay.grid(row=3, column=0, padx=5, pady=5)

self.btnDisplay = Button(self, text=2, default=ACTIVE)
self.btnDisplay.grid(row=3, column=1, padx=5, pady=5)

self.btnDisplay = Button(self, text=3, default=ACTIVE)
self.btnDisplay.grid(row=3, column=2, padx=5, pady=5)

self.btnDisplay = Button(self, text=+, default=ACTIVE)
self.btnDisplay.grid(row=3, column=3, padx=5, pady=5)

self.btnDisplay = Button(self, text=4, default=ACTIVE)
self.btnDisplay.grid(row=4, column=0, padx=5, pady=5)

self.btnDisplay = Button(self, text=5, default=ACTIVE)
self.btnDisplay.grid(row=4, column=1, padx=5, pady=5)

self.btnDisplay = Button(self, text=6, default=ACTIVE)
self.btnDisplay.grid(row=4, column=2, padx=5, pady=5)

self.btnDisplay = Button(self, text=-, default=ACTIVE)
self.btnDisplay.grid(row=4, column=3, padx=5, pady=5)

self.btnDisplay = Button(self, text=7, default=ACTIVE)
self.btnDisplay.grid(row=5, column=0, padx=5, pady=5)

self.btnDisplay = Button(self, text=8, default=ACTIVE)
self.btnDisplay.grid(row=5, column=1, padx=5, pady=5)

self.btnDisplay = Button(self, text=9, default=ACTIVE)
self.btnDisplay.grid(row=5, column=2, padx=5, pady=5)

self.btnDisplay = Button(self, text=*, default=ACTIVE)
self.btnDisplay.grid(row=5, column=3, padx=5, pady=5)

self.btnDisplay = Button(self, text=0, default=ACTIVE)
self.btnDisplay.grid(row=6, column=0, padx=5, pady=5)

self.btnDisplay = Button(self, text=C, default=ACTIVE)
self.btnDisplay.grid(row=6, column=1, padx=5, pady=5)

self.btnDisplay = Button(self, text=r, default=ACTIVE)
self.btnDisplay.grid(row=6, column=2, padx=5, pady=5)

self.btnDisplay = Button(self, text=/, default=ACTIVE)
self.btnDisplay.grid(row=6, column=3, padx=5, pady=5)

def Display(self, xbtn):
if xbtn==1:
print 1

if __name__ == __main__:
guiFrame = GUIFramework()
guiFrame.mainloop()

[/code]

   If you have this function:

   def f():
  print 1
  return 10

   and you write:

   result = f()

   The '()' is the function execution operator; it tells python to
   execute the function.  In this case, the function executes, and then
   the return value of the function is assigned to the variable result.
   If a function does not have a return statement, then the function
   returns None by default.

   The same thing is happening in this portion of your code:

   command = self.Display(1)

   That code tells python to execute the Display function and assign the
   function's return value to the variable command.  As a result Display
   executes and 1 is displayed.  Then since Dispay does not have a return
   statement, None is returned, and None is assigned to command.
   Obviously, that is not what you want to do.

   What you want to do is assign a function reference to command so
   that python can execute the function sometime later when you click on
   the button.  A function reference is just the function name without
   the '()' after it.  So you would write:

   command = self.Display

   But writing it like that doesn't allow *you* to pass any