Python 图形化命令执行器脚本
Python 图形化命令执行器脚本
本脚本使用 Python 语言编写,可以在图形化界面下执行系统命令并回显结果。以下是使用步骤:
- 打开脚本,进入图形化界面。
- 在命令输入框中输入需要执行的命令。
- 点击'执行'按钮,系统将会执行命令并将结果回显在文本框中。
注意事项:
- 请确保您输入的命令是正确的,否则可能会导致系统崩溃。
- 请勿在命令输入框中输入敏感的命令,否则可能会造成系统安全问题。
代码实现
以下是本脚本的代码实现,您可以根据需要进行修改和扩展。
import tkinter as tk
import subprocess
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.grid()
self.create_widgets()
def create_widgets(self):
self.commandLabel = tk.Label(self, text='请输入命令:')
self.commandLabel.grid(row=0, column=0)
self.commandEntry = tk.Entry(self)
self.commandEntry.grid(row=0, column=1)
self.executeButton = tk.Button(self, text='执行', command=self.executeCommand)
self.executeButton.grid(row=0, column=2)
self.resultLabel = tk.Label(self, text='执行结果:')
self.resultLabel.grid(row=1, column=0)
self.resultText = tk.Text(self, width=50, height=20)
self.resultText.grid(row=2, column=0, columnspan=3)
def executeCommand(self):
command = self.commandEntry.get()
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
self.resultText.insert(tk.END, result.stdout.decode('utf-8'))
self.resultText.insert(tk.END, result.stderr.decode('utf-8'))
root = tk.Tk()
app = Application(master=root)
app.mainloop()
原文地址: https://www.cveoy.top/t/topic/lmrO 著作权归作者所有。请勿转载和采集!