Shwetanshu RAj
Shwetanshu RAj

Reputation: 27

How to run a sample code which I get as input from Python GUI?

I am working on a mini-project where I take a simple python function from the user using Tkinter(textbox widget) and then execute it and plot its time taken to execute with change in input value. How can I execute the code taken as input from the textbox widget?

Or should I go with some predefined functions based on general algorithms.

Upvotes: 0

Views: 786

Answers (1)

ruohola
ruohola

Reputation: 24107

Get the text from the whole TextBox with:

text = self.my_text_box.get("1.0", tk.END)

And then run the code with:

exec(text)

But be aware than this allows the user to run ANY CODE he wants.

Upvotes: 2

Related Questions