jarvisteach / appJar

Simple Tkinter GUIs in Python

Home Page:http://appJar.info

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Subwindow to show output of print() command

karkraeg opened this issue · comments

Is it possible to open a kind of console window as a subwindow of an appjar app to display print() commands from a for loop? Like when I`m having

def do_something(x):
    sleep(10)
    print("Filename:" + x)

for x in mylist_of_files:
    do_something(x)

and I want to show the print() output in a app.startSubWindow that mimics a terminal/console.

Is something like this possible? Thanks!

Something "like this" is possible - but you'd never be using print()

You could create a label or message and then call .setMessage() or .setLabel() to put text in them...

NB. when running a loop, it will cause GUIs to hang, so make sure to run it in a separate thread, see here for more info: http://appjar.info/pythonLoopsAndSleeps/

Thanks for your answer! I´m already using app.setStatusbar in an app.registerEvent for continuously updating text, but I wanted a "real" console where one could see runtime errors and debugging messages.

I was looking for something like this with tK:

import tkinter as tk
import time
import sys


class Display(tk.Frame):
    def __init__(self):
        tk.Frame.__init__(self)
        self.doIt = tk.Button(
            self, text="Start", command=self.start, background='black', fg='white')
        self.doIt.pack()
        self.output = tk.Text(self, width=100, height=15,
                              background='black', fg='white')
        self.output.pack()
        sys.stdout = self
        self.configure(background='black')
        self.pack()

    def start(self):
        for i in range(1, 10):
            print("Hello " + str(i))
            time.sleep(1)

    def write(self, txt):
        self.output.insert(tk.END, str(txt))
        self.update_idletasks()


if __name__ == '__main__':
    Display().mainloop()

notice the print("Hello " + str(i)).

Ah, OK, I see what you're getting at now...

This is not really an appJar feature. What you're trying to do is change the way print() works.

In the code you've posted the line sys.stdout = self is doing that. Whenever you call print() it's really calling the write() function in sys.stdout, so that's all you need to do in your code.

If you have a look at this answer: https://stackoverflow.com/a/31388442/5696846

It gives a lot more detail.

But, I do like the concept, and think it would be useful to document in appJar, and maybe even add a built-in feature that can be enabled to support it...