0

I've tried to search this topic up, but it has been very confusing, as I don't use/quite understand stdout/stderr... However, I have this code right now that prints a list onto the console. Can someone explain to me how I could get this to print directly onto a GUI Textbox? My code:

from tkinter import *
import math

class TimeGenerator:

    def __init__(self,master):
        frame = Frame(master)
        frame.grid()
        label_iso = Label(root, text="Isotope A, Element")
        label_vol = Label(root, text="Voltage")
        label_range = Label(root, text="Charge Range")

        entry_iso = Entry(root)
        entry_vol = Entry(root)
        entry_range = Entry(root)

        label_iso.grid(row=0, sticky=E)
        label_vol.grid(row=1, sticky=E)
        label_range.grid(row=2, sticky=E)

        entry_iso.grid(row=0, column=1)
        entry_vol.grid(row=1, column=1)
        entry_range.grid(row=2,column=1)

        button = Button(root, text='Time Range', command=self.calculateTime)
        button.grid(row=3, columnspan=2)

        self.iso = entry_iso
        self.vol = entry_vol
        self.r = entry_range

    def calculateTime(self):
        x = 5

        self.iso = self.iso.get().replace(" ", "")
        list = []
        for e in self.iso.split(","):
            list.append(e)

        f = open("/Users/LazyLinh/PycharmProjects/mass.mas12.txt", "r")
        i = 0
        while (i < 40):
            header = f.readline()
            i += 1
        self.mass = 0

        for line in f:
            line = line.strip()
            columns = line.split()
            if (list[0] == columns[3]):
                if (list[1] == columns[4]):
                    if (len(columns) == 16):
                        self.mass = float(columns[13].replace("#","")) + float(columns[14].replace("#",""))
                    else:
                        self.mass = float(columns[12].replace("#","")) + float(columns[13].replace("#",""))

        self.r = self.r.get().replace(" ", "")
        tup = tuple(int(x) for x in self.r.split(","))

        list = []
        for q in range(tup[0], tup[1] + 1):
            y = x * math.sqrt(self.mass / (2 * q * float(self.vol.get())))
            list.append(y)
        i = tup[0]
        for time in list:
            print(i, ':', time)
            i = i + 1


root = Tk()
b = TimeGenerator(root)
root.mainloop()

Thank you!

Linh Phan
  • 83
  • 1
  • 9
  • You may be able to adapt the `errorwindow` module in [this answer](http://stackoverflow.com/questions/18089506/writing-to-easygui-textbox-as-function-is-running-python/18091356#18091356) which redirects both `stdout` as well as `stderr` to another window. – martineau May 16 '16 at 18:47
  • You don't have any sort of "Textbox" in your UI. Where do you want the text to go? – Bryan Oakley May 16 '16 at 19:07
  • @martineau: I don't think there's any reason to do redirection; the OP can just replace `print` with an appropriate function. – Bryan Oakley May 16 '16 at 19:08
  • @BryanOakley: I will have to instantiate a Textbox, but can you please explain your second command of replacing print with a different function? Would I then pass that function to a textbox object? How can that be done? Thank you!!! – Linh Phan May 16 '16 at 19:10

1 Answers1

0

Somewhere in your code you need to create a text widget:

class TimeGenerator:

    def __init__(self,master):
        ...
        self.text = Text(...)
        ...

Later, use the insert method of the text widget instead of a print statement:

    for time in list:
        self.text.insert("end", "%d: %s\n" % (i, time))
        i = i + 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685