Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- from Tkinter import Tk, Button
- from Tkconstants import END
- import tkFileDialog
- import ScrolledText
- from threading import Thread
- from Queue import Queue
- class RunThread(Thread):
- def __init__(self, filepath):
- super(RunThread, self).__init__()
- self.filepath = filepath
- self.output = Queue()
- self.finished = False
- def run(self):
- p = subprocess.Popen(['python', self.filepath],
- stdout=subprocess.PIPE,
- stderr=subprocess.STDOUT,
- )
- for line in iter(p.stdout.readline, ''):
- self.output.put(line)
- self.finished = True
- class Application(object):
- run_thread = None
- def run(self):
- self.root = Tk()
- self.code = ScrolledText.ScrolledText(self.root)
- self.code.pack()
- self.output = ScrolledText.ScrolledText(self.root)
- self.output.pack()
- self.run = Button(self.root, text="Run", command=self.run_script)
- self.run.pack()
- self.root.mainloop()
- def run_script(self):
- if self.run_thread:
- return
- script = self.code.get(1.0, END)
- filepath = tkFileDialog.asksaveasfilename()
- if filepath:
- if not filepath.endswith('.py'):
- filepath += '.py'
- with open(filepath, 'w') as f:
- f.write(script)
- self.run_thread = RunThread(filepath)
- self.run_thread.start()
- self.read_output()
- def read_output(self):
- while not self.run_thread.output.empty():
- line = self.run_thread.output.get()
- self.output.insert(END, line)
- if self.run_thread.finished:
- self.output.insert(END, "%s Finished" % self.run_thread.filepath)
- self.run_thread = None
- else:
- self.root.after(100, self.read_output)
- Application().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement