简体   繁体   中英

how to get the console output into a label in tkinter

So im trying to make a simple pyscript editor and i want to get the console output into a label inside my tkinter wiondow

here is my code so far Thanks!

from tkinter import Text, Tk
import os
root = Tk()
root.configure(background='black')
root.geometry('1920x1080')
def ok():
    input = text1.get("1.0","end-1c")
    gay = open('data.py', 'w')
    gay.write(input)
    gay.close()
    os.startfile('data.py')


text1 = Text(root, bg="#6B6B6B", height=50, width=200)
text1.place(x=320, y=40)
but1 = Button(root, text="halleo", command=ok).pack()
root.mainloop()

I think this is what you are after... It is hard to tell from your question. I assume by console you mean text area. I took out the file output, but you could put it back in.

from tkinter import *
import subprocess
import shlex


def run_command(the_command):
    process = subprocess.Popen(shlex.split(the_command), stdout=subprocess.PIPE)
    output = process.stdout.read()
    output = str(output).replace("b'", "").replace("\\n'", "")
    lab1.configure(text=output)


def command():
    the_input = text1.get("1.0", "end-1c")
    run_command(the_input)


root = Tk()
root.configure(background='black')
root.geometry('1920x1080')
text1 = Text(root, bg="#6B6B6B", height=50, width=200)
text1.place(x=320, y=40)
but1 = Button(root, text="halleo", command=command).pack()
lab1 = Label(root, text="asdf", background='white')
lab1.pack()

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM