python - Tkinter 线程问题:system() 最多接受 1 个参数(给定 27 个)
问题描述
我有这个代码来安装 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()
但它会引发错误;TypeError: system() 最多接受 1 个参数(给定 27 个)我可以做到os.system(f'cmd /c "{command}"')
,但按钮会冻结,所以我不想要那个。如何修复我的错误?
解决方案
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()
试试这个,看看。
推荐阅读
- typescript - TypeScript:Jest 测试中的类型上不存在属性“mockImplementation”
- javascript - 如何调整已使用 `alignItems` 和 `justifyContent` 垂直和水平居中的组件的位置?
- matlab - 矩阵对角线上的高斯包络
- c++ - Ostream 和 istream 作为构造函数的争论
- java - 收到n个元素后如何退出akka流?
- python - 如何使用 gremlin 和 python 在海王星中进行组合布尔运算?
- c# - EF Core 获取整个对象以进行空检查
- c - 如何在C中为数组动态分配内存?
- rust - 无法让 RwLock 在多线程应用程序中工作
- c# - 是否可以在创建 zip 文件时获取其大小?CreateEntryFromFile