0

I have this program written in python tkinter, which has a text box and a menu. The menu has two options, open file and run file.

The open file lets you open python files and writes the contents of the file into the text box. The run file opens up a file dialog and lets you select a python file to run.

I tried to make it so that when you press the run file button the program will run the currently opened file instead of creating a new file dialog which asks you to select a file to run. However, I ran into a problem doing this.

This is my code so far:

# Imports
from tkinter import *
from tkinter import filedialog


# Window
root = Tk()
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))


# Global OpenStatusName - used for finding name and status of opened file and use it for saving file and etc
global OpenFileStatusName
OpenFileStatusName = False


# Open File Function
def OpenFile(*args):
    # Ask user for which file they want to open
    FilePath = filedialog.askopenfilename(initialdir="C:/gui/", title="Open a File", filetypes=(("All Files", "*.*"), ("Text Files", "*.txt"), ("HTML Files", "*.html"), ("CSS Files", "*.css"),("JavaScript Files", "*.js"), ("Python Files", "*.py")))
    
    # Check to see if there is a file opened, then find the name and status of the file and use it in code for other things like saving a file and accessing it later
    if FilePath:
        global OpenFileStatusName
        OpenFileStatusName = FilePath
    
    # Delete Any Previous Text from the TextBox
    TextBox.delete("1.0", END)
    
    # Open File and Insert File Content into Editor
    FilePath = open(FilePath, 'r')
    FileContent = FilePath.read()
    TextBox.insert(END, FileContent)
    FilePath.close()


# Run Python Menu Options
def RunPythonFile():
    OpenFileToRun = filedialog.askopenfile(mode="r", title="Select Python File to Run")
    exec(OpenFileToRun.read())


# Main Frame for Placing the Text Box
MainFrame = Frame(root)
MainFrame.pack()


# Text Box
TextBox = Text(MainFrame, width=500, undo=True)
TextBox.pack(fill=BOTH)


# Menu Bar
MenuBar = Menu(root)
root.config(menu=MenuBar)


# File Option for Menu Bar
FileMenu = Menu(MenuBar, tearoff=False)
MenuBar.add_cascade(label="File", menu=FileMenu)
FileMenu.add_command(label="Open", command=OpenFile)
FileMenu.add_command(label="Run File", command=RunPythonFile)


# Mainloop
root.mainloop()

Instead of OpenFileToRun = filedialog.askopenfile(mode="r", title="Select Python File to Run") exec(OpenFileToRun.read()) in the RunPythonFile Function is there anything else that I could put so the program would only run the currently opened file?

Skcoder
  • 299
  • 1
  • 9

2 Answers2

0

Just pass the content of TextBox to exec():

# Run Python Menu Options
def RunPythonFile():
    exec(TextBox.get("1.0", "end-1c"))

Note that using exec() to execute code is not recommended.

acw1668
  • 40,144
  • 5
  • 22
  • 34
  • 1
    "Any proposed solution beginning with the word 'just' is immediately suspect." -- Holden's Second Law. – holdenweb May 14 '21 at 08:47
  • @acw1668 - Thank you very much, that really helps! But could you explain why using exec() to execute code is not recommended? What else could you do to execute code correctly/efficiently? – Skcoder May 14 '21 at 12:37
  • For your case, it is ok because you load the content into the text box so that you can check whether the code is dangerous or not. But if you just load the file into a variable and then execute it, then it is dangerous. – acw1668 May 14 '21 at 12:48
  • Also could you please answer this as well: how do you get the file type when a file is opened using the Python filedialog askopenfilename() method? https://stackoverflow.com/questions/67542121/how-do-you-get-the-file-type-when-a-file-is-opened-using-the-python-filedialog-a – Skcoder May 14 '21 at 23:57
0

This reads the content of the file

OpenFileToRun.read()

Using subprocess it will give you plenty of options. You can use .wait, subprocess.call, or check_call and so on.

import subprocess as sp

# Run Python Menu Options
def RunPythonFile():
    OpenFileToRun = filedialog.askopenfilename(initialdir="Path",title="Select Python File to Run",filetypes = (("python files","*.py"),("all files","*.*")))
    nextProg = sp.Popen(["python",OpenFileToRun])
Daniel
  • 61
  • 1
  • 6