Python Forum

Full Version: How to show terminal output in GUI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi There,
I am looking for a easily way to show my terminal output directly in the created GUI. My code looks as follows:
import sys

from tkinter import *

def TimesTable():
    print("\n")
    for x in range(1,13):
        m = int(EnterTable.get())
        print('\t\t', (x), ' x ',(m), ' = ', (x * m),)

Multiply = Tk()
Multiply.geometry('250x500+700+200')
Multiply.title('Multiplication Table')

EnterTable = StringVar()

label1=Label(Multiply, text='Multiplication Times Table', font=30, fg='Black').grid(row=1, column=6)
label1=Label(Multiply,text='                                         ').grid(row=2,column=6)
entry5=Entry(Multiply, textvariable=EnterTable, justify='center').grid(row=3, column=6)
label1=Label(Multiply,text='                                         ').grid(row=4,column=6)            

button1=Button(Multiply, text='Times Table', command=TimesTable).grid(row=5,column=6)
label1=Label(Multiply,text='                                         ').grid(row=6,column=6)        
QUIT=Button(Multiply,text='Quit', fg='Red', command=Multiply.destroy).grid(row=7,column=6)

Multiply.mainloop()
Has anyone a good solution for this?
Thanks a lot,
Rubberduck
You can use contextlib redirect. I use this in my Qt based Python console.

https://python-forum.io/thread-25117.html
Thanks a lot @deanhystad

I already could solve it by adding two to tree lines code. With this solution I am happy for the moment.

import sys

from tkinter import *

def TimesTable():
    print("\n")
    result = "Result"
    for x in range(1,13):
        m = int(EnterTable.get())
        print('\t\t', (x), ' x ',(m), ' = ', (x * m),)
        result = result + '\t\t' + str(x) + ' x ' + str(m)+  ' = ' + str(x * m) + "\n"
    result=Label(Multiply, text=result, justify='left').grid(row=9, column=6)


Multiply = Tk()
Multiply.geometry('250x500+700+200')
Multiply.title('Multiplication Table')

EnterTable = StringVar()

label1=Label(Multiply, text='Multiplication Times Table', font=30, fg='Black').grid(row=1, column=6)
label1=Label(Multiply,text='                                         ').grid(row=2,column=6)
entry5=Entry(Multiply, textvariable=EnterTable, justify='center').grid(row=3, column=6)
label1=Label(Multiply,text='                                         ').grid(row=4,column=6)            

button1=Button(Multiply, text='Times Table', command=TimesTable).grid(row=5,column=6)
label1=Label(Multiply,text='                                         ').grid(row=6,column=6)        
QUIT=Button(Multiply,text='Quit', fg='Red', command=Multiply.destroy).grid(row=7,column=6)
label1=Label(Multiply,text='                                         ').grid(row=8,column=6)        
result=Label(Multiply, text="Show result by insert a value", justify='left').grid(row=9, column=6)
label1=Label(Multiply,text='                                         ').grid(row=10,column=6)        


Multiply.mainloop()
Best Wishes,
Rubberduck