简体   繁体   中英

How to run currently opened file in python tkinter?

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?

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.

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM