Is there possible to open and run any file in tkinter? I want to open txt file by clicking a button in application. Like this picture below
Is there any way?
Is there possible to open and run any file in tkinter? I want to open txt file by clicking a button in application. Like this picture below
Is there any way?
you could either use subprocess.Popen() or if it is a txt file, then open it with f = file.open("path/name.txt","r or w or a"), Iam not sure how to be confident with your case though
import tkinter as tk
def on_click(event):
root.destroy()
with open("somefile.py") as f:
code = compile(f.read(), "/path/to/my/file.py", 'exec')
exec(code, global_vars, local_vars)
root = tk.Tk()
button = tk.Button(root, text="Run")
button.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
button.bind("<Button-1>", on_click)
tk.mainloop()
This will put the button in the middle of the window, and when clicked, will run "/path/to/my/file.py". If you are using Python2, on_click()
can be simplified to this:
def on_click(event):
root.destroy()
execfile("/path/to/my/file.py")
from Tkinter import *
from tkFileDialog import askopenfilename
def openfile():
filename = askopenfilename(parent=root)
f = open(filename)
f.read()
root = Tk()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=openfile)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
root.mainloop()
use this code to open a file