Python - Problema de subprocesamiento de Tkinter: system() toma como máximo 1 argumento (27 dados)

CorePress2024-01-25  11

Tengo este código para instalar Pygame.

import os
import tkinter as tk
import threading

root = tk.Tk()

canvas1 = tk.Canvas(root, width=300, height=300, bg='gray90', relief='raised')
canvas1.pack()


def run_command(command):
    # for i in range (5):
    threading.Thread(target=os.system, args=f'cmd /c "{command}"').start()  # TypeError: system() takes at most 1
    # argument (27 given)


button1 = tk.Button(text='      Run Command      ', command=lambda: run_command("pip install pygame"), bg='green', fg='white',
                    font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 150, window=button1)

root.mainloop()

Pero arroja un error; TypeError: system() toma como máximo 1 argumento (27 dados) Puedo hacer os.system(f'cmd /c "{command}"'), pero el botón se congela, así que no quiero eso. ¿Cómo puedo solucionar mi error?



------------------------------------

El error se produce porque los "args" argumento en threading.Thread espera una tupla.

Cambiando la línea a threading.Thread(target=os.system, args=(f'cmd /c "{command}"', )).start() soluciona el problema.

Código completo:

import os
import tkinter as tk
import threading

root = tk.Tk()

canvas1 = tk.Canvas(root, width=300, height=300, bg='gray90', relief='raised')
canvas1.pack()


def run_command(command):
    # for i in range (5):
    print(os.system)
    threading.Thread(target=os.system, args=(f'cmd /c "{command}"', )).start()  # TypeError: system() takes at most 1
    # argument (27 given)


button1 = tk.Button(text='      Run Command      ', command=lambda: run_command("pip install pygame"), bg='green', fg='white',
                    font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 150, window=button1)

root.mainloop()



------------------------------------

import tkinter as tk
import threading

root = tk.Tk()

canvas1 = tk.Canvas(root, width=300, height=300, bg='gray90', relief='raised')
canvas1.pack()


def run_command(command):
    # for i in range (5):
    threading.Thread(target=lambda: os.system(command)).start()


button1 = tk.Button(text='      Run Command      ', command=lambda: run_command("pip install pygame"), bg='green', fg='white',
                    font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 150, window=button1)

root.mainloop()

prueba esto y verás.

Su guía para un futuro mejor - libreflare
Su guía para un futuro mejor - libreflare