0

I have a label that I am setting a random number to.

output.set(randint(1,4))

output = StringVar()
ttk.Label(mainframe, textvariable=output)

I also have an Entry where the user can specify how many random numbers they want to generate.

ttk.Entry(mainframe, textvariable=numberdice, width=5)

What I am trying to do is to get the two values to work together, and have the output label print multiple lines.

I got it to print to the console using a for loop:

for x in range(int(numberdice)):
    print(randint(1,4))

But I cant get it to display the same in the GUI...

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
alloneword170
  • 11
  • 1
  • 2
  • @TigerhawkT3 I'm not sure that dup target answers this question – Tadhg McDonald-Jensen Feb 13 '17 at 02:08
  • first understand that `"\n"` is a "newline" character that separates lines, `print` adds it to the end of output for you (unless you specify something else: `print(5, end="HELLO")`) you can get the whole string yourself by doing `"\n".join(randint(1,4) for x in range(int(numberdice.get())))` then do `output.set()` on that value. – Tadhg McDonald-Jensen Feb 13 '17 at 02:14
  • @TadhgMcDonald-Jensen I tried your suggestion and it returned an error `sequence item 0: expected str instance, int found` which 1) doesn't make sense and 2) correcting to an str breaks it more. I do have the numberdice Entry set to a StringVar so I'm assuming it has something to do with that.. – alloneword170 Feb 14 '17 at 00:21
  • I don't remember, but can you even put multiple lines on a Tkinter label? I don't think that's possible. I'd use a listview or something. – Wayne Werner Feb 14 '17 at 16:51

1 Answers1

0

How to use the print function to display to a GUI

You can create a StringVar that supports write so you can print to it like a file:

import tkinter as tk

class WritableStringVar(tk.StringVar):
    def write(self, added_text):
        new_text = self.get() + added_text
        self.set(new_text)

    def clear(self):
        self.set("")

Then using print(... , file=textvar) you can add to the string variable exactly the same way as printing to the console:

root = tk.Tk()

textvar = WritableStringVar(root)

label = tk.Label(root, textvariable=textvar)
label.pack()

for i in range(4):
    print("hello there", file=textvar)

root.mainloop()

demo app

note that because print statements (by default) add a newline at the end there will be an extra blank line at the bottom of the label, if it bugs you it can be worked around by removing the newline and reinserting it next addition:

class WritableStringVar(tk.StringVar):
    newline = False
    def write(self, added_text):
        new_text = self.get()
        if self.newline: #add a newline from last write.
            new_text += "\n"
            self.newline = False
        if added_text.endswith("\n"): #remove this newline and remember to add one next write.
            added_text = added_text[:-1]
            self.newline = True
        new_text += added_text
        self.set(new_text)

    def clear(self):
        self.set("")
        self.newline = False

Either way you can make use of prints convenient semantics to get text on your GUI.

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59