我可以在 Python 中制作启动 Python 代码行的 GUI 吗?
Can i make GUI in Python that launches Python code line?
有没有像这样工作的 GUI 程序 - 单击 GUI 中的按钮,它确实启动了一些 python 代码行或多行,我考虑过 tkinter,但我找到了 none 选项让它启动一些 python 代码还是有?
更新 - 在终端代码中使用代码会太复杂,所以我需要 GUI。
运行python代码有几种方式,OP中要求的方式-:
使用 exec
或 eval
到 运行 python 动态编码(注意: 这将不是
return 输出,而是直接 运行 代码,就好像它是
正常代码。),还要注意 use of exec
and eval
should generally be avoided 但必须在此处提及它作为一种可能的方式。
exec(object, globals, locals)
eval(expression[, globals[, locals]])
启动终端 window(使用 os.system
method of the os module) and using pyautogui 键入和 运行
执行给定 python 脚本的命令,当按下按钮时
单击(注意: 使用此方法您也只能看到
脚本的输出但不获取它。)
# required imports
import os, pyautogui
import tkinter as tk
from tkinter import filedialog
root = tk.Tk() # initializing the tkinter window.
def ask_for_running_file(event = None) :
filetypes = (("python files", '.py'), ("all files","*.*"))
filename = filedialog.askopenfilename(initialdir = '/', title = "Select file", filetypes = filetypes) # Get the name of the
python file to run.
addr_command = 'start cmd /k "cd "' + filename[ : filename.rindex('/')] # Command used to open the terminal
run_command = ' python \"' + filename[(filename.rindex('/') + 1) : ] + '\"' # The command to be typed in the terminal using pyautogui to execute the python script.
os.system(addr_command) # Runs the command required to open the terminal window.
pyautogui.write(run_command, interval = 0.25) # Writes the command required to execute the python script letter by letter.
NOTE: The interval here can be adjusted as per use case.
pyautogui.press('enter') # Pressing enter to execute the run command.
return
b1 = tk.Button(root, text = 'run file', command = ask_for_running_file) # The button that open a file dialog to choose
#which file to run.
b1.pack() # managing the geometry of the button.
root.mainloop() # starting mainloop
使用subprocess模块获取a执行的输出
终端命令,然后使用 tkinter 或任何其他方式显示它
模块(如果需要)。请注意,子流程模块建议使用
大多数用例的 run 函数因此提供的示例
下面将只涉及到它的使用。该文档列出了每个参数的用法,可以根据需要进一步修改以下代码。
result = subprocess.run(['python', '\"' + script_name + '\"'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell = True)
一旦确定了执行命令的方式,触发它的按钮和GUI就可以在任何模块中随意设计,例如方法2中所示的功能可以与任何其他GUI模块的按钮一起使用还有回调函数!它应该可以正常工作,记住在函数中删除 tkinter filedialog 的使用 rest 可以保持不变。
注意:
如果在pyautogui键入命令时出现干扰,第二种方法可能并不总是有效,因此它不适合需要后台进程来实现问题解决的用例。
有没有像这样工作的 GUI 程序 - 单击 GUI 中的按钮,它确实启动了一些 python 代码行或多行,我考虑过 tkinter,但我找到了 none 选项让它启动一些 python 代码还是有? 更新 - 在终端代码中使用代码会太复杂,所以我需要 GUI。
运行python代码有几种方式,OP中要求的方式-:
使用
exec
或eval
到 运行 python 动态编码(注意: 这将不是 return 输出,而是直接 运行 代码,就好像它是 正常代码。),还要注意 use ofexec
andeval
should generally be avoided 但必须在此处提及它作为一种可能的方式。exec(object, globals, locals) eval(expression[, globals[, locals]])
启动终端 window(使用
os.system
method of the os module) and using pyautogui 键入和 运行 执行给定 python 脚本的命令,当按下按钮时 单击(注意: 使用此方法您也只能看到 脚本的输出但不获取它。)# required imports import os, pyautogui import tkinter as tk from tkinter import filedialog root = tk.Tk() # initializing the tkinter window. def ask_for_running_file(event = None) : filetypes = (("python files", '.py'), ("all files","*.*")) filename = filedialog.askopenfilename(initialdir = '/', title = "Select file", filetypes = filetypes) # Get the name of the python file to run. addr_command = 'start cmd /k "cd "' + filename[ : filename.rindex('/')] # Command used to open the terminal run_command = ' python \"' + filename[(filename.rindex('/') + 1) : ] + '\"' # The command to be typed in the terminal using pyautogui to execute the python script. os.system(addr_command) # Runs the command required to open the terminal window. pyautogui.write(run_command, interval = 0.25) # Writes the command required to execute the python script letter by letter. NOTE: The interval here can be adjusted as per use case. pyautogui.press('enter') # Pressing enter to execute the run command. return b1 = tk.Button(root, text = 'run file', command = ask_for_running_file) # The button that open a file dialog to choose #which file to run. b1.pack() # managing the geometry of the button. root.mainloop() # starting mainloop
使用subprocess模块获取a执行的输出 终端命令,然后使用 tkinter 或任何其他方式显示它 模块(如果需要)。请注意,子流程模块建议使用 大多数用例的 run 函数因此提供的示例 下面将只涉及到它的使用。该文档列出了每个参数的用法,可以根据需要进一步修改以下代码。
result = subprocess.run(['python', '\"' + script_name + '\"'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell = True)
一旦确定了执行命令的方式,触发它的按钮和GUI就可以在任何模块中随意设计,例如方法2中所示的功能可以与任何其他GUI模块的按钮一起使用还有回调函数!它应该可以正常工作,记住在函数中删除 tkinter filedialog 的使用 rest 可以保持不变。
注意:
如果在pyautogui键入命令时出现干扰,第二种方法可能并不总是有效,因此它不适合需要后台进程来实现问题解决的用例。