Tkinter label output using multiline

Post here if you need help with creating a Graphical User Interface in Python.

Tkinter label output using multiline

Postby evol_w10lv » Sat Jun 01, 2013 8:09 am

Hi!
I have got problem, when I am using Tkinter label, it shows just last result of loop. Like this:
Image
But I want see in the label all results. I made picture using Paint with potencial result:
Image

I hope you understand, what result of programm I expect.
Here is my code:
Code: Select all
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


"\n" doesn't give result wich I want to see. What's wrong there? Any suggestions?
evol_w10lv
 
Posts: 1
Joined: Sat Jun 01, 2013 8:03 am

Re: Tkinter label output using multiline

Postby metulburr » Sat Jun 01, 2013 8:48 am

I would normally rewrite this, but i am currently too tried at this point. What is up with all the magic numbers?

so what i added was a string, which adds whatever that math smorgasbord string to that, and set var. You had it setting the var every loop, which only accounted for that last loop.
Code: Select all
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():
     s = ''
     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
          stringer = ('%s %s %s')%(w,K,Y)
         
          s += ('%s %s %s')%(w,K,Y)
          s += '\n'
         
          root.update()
          print ('%s %s %s')%(w,K,Y)
          datu_fails.write(('%s %s %s\n')%(w,K,Y))
     var.set(s)
label=Label(root,textvariable=var)
label.pack()
start_button=Button(root,text="Poga", command=poga)
start_button.pack()
root.mainloop()
datu_fails.close
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1325
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Tkinter label output using multiline

Postby wuf » Sun Jun 02, 2013 8:46 pm

Hi evol_w10lv

Here a solution with textbox and format:
Code: Select all
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():
     s = ''
     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
          stringer = ('%s %s %s')%(w,K,Y)
         
          #s += ('%s %s %s')%(w,K,Y)
          s += '{0:.2f}  {1:.12f}  {2:.12f}'.format(w,K,Y)
          s += '\n'
         
          root.update()
          print ('%s %s %s')%(w,K,Y)
          datu_fails.write(('%s %s %s\n')%(w,K,Y))
     #var.set(s)
     text_box.insert('1.0', s)
#label=Label(root,textvariable=var)
#label.pack()

display_frame = Frame(root)
display_frame.pack()

scrollbar = Scrollbar(display_frame)
scrollbar.pack(side='right', fill='y')

text_box = Text(display_frame, yscrollcommand=scrollbar.set, width=50)
text_box.pack(side='left')

scrollbar.config(command=text_box.yview)

start_button=Button(root,text="Poga", command=poga)
start_button.pack()
root.mainloop()
datu_fails.close

wuf ;)
wuf
 
Posts: 37
Joined: Fri Feb 08, 2013 6:42 am


Return to GUI

Who is online

Users browsing this forum: grim of doom and 1 guest