Home > Mobile >  Can i make GUI in Python that launches Python code line?
Can i make GUI in Python that launches Python code line?

Time:03-06

Is there any GUI programs that work like this - click button in GUI and it does launch some python code line or multiple lines, i thought about tkinter, but i found none options to make it launch some python code or is there? Upd - In terminal code will be too complicated to use the code so i need GUI.

CodePudding user response:

There are a few ways to run python code, the way it is required in the OP -:

  1. Using exec or eval to run python code dynamically(NOTE: This would not return the output but rather directly run the code as if it were normal code.), Also note that use of exec and eval should generally be avoided but it had to be mentioned here as a possible way through.

    exec(object, globals, locals)
    eval(expression[, globals[, locals]])
    
  2. Launching a terminal window(using os.system method of the os module) and using pyautogui to type and run the command that executes given python script, when a button is clicked(NOTE: With this method as well you will only be able to see the output of the script but not fetch it.)

    # 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
    
  3. Use subprocess module to get the output of the execution of a terminal command and then display it using tkinter or any other module(if required). Note that the subprocess module suggests to use the run function for most of the use cases so the example provided below will involve the use of that only. The documentation lists the use of each and every argument and can be used further to modify the below code as pleased.

     result = subprocess.run(['python', '\"'   script_name   '\"'],
            stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell = True)
    

Once the way of executing the command is figured out, the button that triggers it and the GUI can be designed in any module as pleased, for example the function shown in method 2 can be used with any other GUI module's button's callback function too! And it should work fine keeping in mind to remove the use of tkinter filedialog within the function rest can stay the same.


NOTE:

The second method might not always work if there is a disturbance while pyautogui types in the command, and thus it is not suitable for use cases where background processes are needed to achieve the problem's solution.

  • Related