Проблема с subprocess в Python
возникла следующая проблема. Запускаю свою программу, которая при нажатии кнопки запускает subprocess с чат-ботом и переводит его консоль в окно. Внутри пайчарм всё прекрасно работает, но вот если запускать скрипт просто как файл .py он не запускает subprocess или запускает неправильно, так и не понял. При попытке взаимодейстововать с чат-ботом, который якобы запущен там в subprocess выдает ошибку:
Exception in thread Thread-1 (start_skinless_mail):
Traceback (most recent call last):
File "C:\Users\Я\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009, in _bootstrap_inner
self.run()
File "C:\Users\Я\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Я\PycharmProjects\SkinlessMail\hran.py", line 50, in start_skinless_mail
output = proc.stdout.readline().decode("utf-8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf0 in position 14: invalid continuation byte
Вот код:
from tkinter import *
import tkinter as tk
import sys
import subprocess
import threading
def main():
root = Tk()
root.geometry('500x240')
root.resizable(width=False,height=False)
frame = Frame(root,bg="#a30a0a")
frame.place(relx=0, rely=0, relwidth=1, relheight=1)
skinm = Button(frame,text="SkinlessMail",bg='#8a0000',font=10,pady=10,padx=5,fg='white',width=25,command=skinlessmail)
skinm.pack(side=LEFT)
root.mainloop()
def skinlessmail():
no = '#a30a0a'
root = Tk()
root.geometry('800x600')
root.resizable(width=False, height=False)
root.columnconfigure(1, weight=1)
root.rowconfigure(1, weight=1)
frame = Frame(root, bg="#a30a0a")
frame.place(relx=0, rely=0, relwidth=1, relheight=1)
def start_skinless_mail():
global proc
proc = subprocess.Popen(["python", "SkinlessMail.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
output = proc.stdout.readline().decode("utf-8")
if not output and proc.poll() is not None:
break
root.console_redirector.write(output)
def run_script():
skinless_mail_thread = threading.Thread(target=start_skinless_mail)
skinless_mail_thread.start()
def close_script():
global proc
proc.kill()
frame.grid_columnconfigure(5, weight=1)
frame.grid_rowconfigure(7, weight=1)
skins = Button(frame, text="Старт", bg='#8a0000', font=10, pady=10, padx=5, fg='white', width=25,command=run_script)
skins.grid(row=2, column=6, pady=4, padx=16)
skins = Button(frame, text="Стоп", bg='#8a0000', font=10, pady=10, padx=5, fg='white', width=25,command=close_script)
skins.grid(row=3, column=6, pady=4, padx=16)
title = Label(frame, text='ИСТОРИЯ', bg=no, font=10, fg='white', width=25)
title.grid(row=6, columnspan=7, pady=8)
class ConsoleRedirector:
def __init__(self, console_text_widget):
self.console_text_widget = console_text_widget
def write(self, message):
self.console_text_widget.configure(state='normal')
self.console_text_widget.insert(tk.END, message)
self.console_text_widget.configure(state='disabled')
self.console_text_widget.see(tk.END)
def flush(self):
pass
root.console_text_widget = tk.Text(root, state='disabled')
root.console_text_widget.grid(row=8, columnspan=7, pady=4)
root.console_text_widget.config(height=15)
root.console_redirector = ConsoleRedirector(root.console_text_widget)
sys.stdout = root.console_redirector
sys.stderr = root.console_redirector
root.mainloop()
main()
Источник: Stack Overflow на русском