繁体   English   中英

如何在 tkinter 中将控制台 output 转换为 label

[英]how to get the console output into a label in tkinter

所以我试图制作一个简单的 pyscript 编辑器,我想让控制台 output 进入我的 tkinter wiondow 中的 label

到目前为止,这是我的代码谢谢!

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()

我认为这就是您所追求的……从您的问题中很难看出。 我假设控制台是指文本区域。 我取出了文件 output,但你可以把它放回去。

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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