Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
May 20th, 2013, 12:58 PM
 evol
Registered User

Join Date: Dec 2012
Posts: 5
Time spent in forums: 1 h 39 m 28 sec
Reputation Power: 0
Python Tkinter label output

Hi!
I have got problem, when I am using Tkinter label, it shows just last result of loop. Like this:

http://www.bildites.lv/images/s99rets056z9c3t3z2nu.png

But I want see in the label all results. I made picture using Paint with potencial result:

http://www.bildites.lv/images/5g2fcv3lkxwb4n7hoj5o.png

I hope you understand, what result of programm I expect.
Here is my code:
Code:
import math
from math import sqrt
from math import atan
from Tkinter import *
import time
root = Tk()
var = StringVar()
b=0.04
a=2
datu_fails = open("sd3.txt", "w")
def poga():
w=0.2-b
while (w<a):
K=(sqrt((-4.8*w**2)**2 + (10*w - 12*w**3)**2))/(sqrt((4-10.96*w**2 + 6*w**4)**2 + (4*w - 4.8*w**3)**2))
Y=atan(2)*(10*w - 12*w**3 -4.8*w**2) - atan(2)*(4*w-4.8*w**3+4-10.96*w**2+6*w**4)
w=w+b
var.set(('%s %s %s')%(w,K,Y))
root.update()
print ('%s %s %s')%(w,K,Y)
datu_fails.write(('%s %s %s\n')%(w,K,Y))
label=Label(root,textvariable=var)
label.pack()
start_button=Button(root,text="Poga", command=poga)
start_button.pack()
root.mainloop()
datu_fails.close

What's wrong there?

#2
May 20th, 2013, 02:50 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,217
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 34 m 54 sec
Reputation Power: 455
You seem to think the variable or Label works in an append mode. It doesn't. The value is successively replaced. Tkinter might permit multi-line labels now, but you really need a Text widget.

http://effbot.org/tkinterbook/text.htm

The methods for accessing row and column confuse me no end. Things I've learned:

Columns and rows don't use the same index origin. That is, the top left is either 1, 0 or 0, 1. Except neither of these is true...because...

A row, column address is given as a string, "row.column". Thus the first position is either
'1.0' or '0.1'

tkinter measurements also drive me nutty. Some sizes are in pixels. Some are determined by the font. I think this is consistent that "text-like" objects (widgets) are measured by font, while "picture-like" widgets (the canvas) are measured in pixels.

Good luck.
__________________
[code]Code tags[/code] are essential for python code!

#3
May 21st, 2013, 12:33 PM
 evol
Registered User

Join Date: Dec 2012
Posts: 5
Time spent in forums: 1 h 39 m 28 sec
Reputation Power: 0
Do you want to say, that it isn't possible to get every result of loop in new line in Label?
Actually I noticed that results are changing in the Label, but I expect to see all results in the Label, but "\n" new line didn't give that result.
Maybe I don't understand the task, but there was clearly said, that have to use "Label" type widget to output results. Or "Label" type widget could be "Text" widget as well?

Can you suggest something?

#4
May 21st, 2013, 12:45 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,217
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 34 m 54 sec
Reputation Power: 455
I said that multi-line Labels might be available. Perhaps in python 3.2. At any rate, you need to append the data instead of over writing it.

information = 'first result'
information = '\n'.join((information, 'second result'))

from which you might then set the value of the Label's string variable.

Then another way to append data, if the preceding method didn't work, is to put new information onto new rows of a Text widget.

And, you could use many labels instead of just the one label.

#5
May 22nd, 2013, 11:27 AM
 dwblas
Contributing User

Join Date: May 2009
Posts: 421
Time spent in forums: 5 Days 7 h 42 m 44 sec
Reputation Power: 32
I would suggest a Listbox.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Python Tkinter label output