Reputation: 306
I wrote a python 3.4.2 programme to get a user input from python IDLE, perform some processing on the input and display a few statements in the python IDLE using print().
Now I am in the process of converting this to use a GUI using tkinter. This is the simple tkinter code I wrote for the GUI.
from tkinter import *
root=Tk()
root.title("Post-fix solver")
root.geometry("500x500")
mainframe=Frame(root)
mainframe.grid(column=0, row=0)
inputval=StringVar()
inputentry=Entry(mainframe,textvariable=inputval).grid(column=1,row=1)
executebutton=Button(mainframe,text="Run",command=check_expression).grid(column=1,row=5)
outputtext=Text(mainframe).grid(column=1,row=5)
root.mainloop()
So far I was able to get the user input through the Entry widget named inputentry in the GUI and send it to a variable in the original code using inputval.get(). Then it performs the processing on the input and shows the outputs of print() statement in the python IDLE.
My question is how can I modify the programme to send all those print() statements to the Text widget named outputtext and display them in the GUI?
I would be glad if you could show me how to do this without using classes as I am a beginner in python
Upvotes: 2
Views: 29407
Reputation: 4543
My question is how can I modify the programme to send all those print() statements to the Text widget named outputtext and display them in the GUI?
The problem can be fixed.
Simply, without adding extra widgets such as Button
. Just add root.bind
outside of the function.
If you want to delete an entry after typing. You use inputentry
instead of outputtext
.
Snippet:
from tkinter import *
root=Tk()
root.title("Post-fix solver")
root.geometry("500x500")
mainframe=Frame(root)
mainframe.grid(column=0, row=0)
def check_expression(event):
varContent = inputentry.get()
inputentry.delete(0, END)
outputtext.insert(END, varContent)
root.bind('<Return>' , check_expression)
inputval=StringVar()
inputentry=Entry(mainframe,textvariable=inputval)
inputentry.grid(column=1,row=1)
executebutton=Button(mainframe,text="Run",command=check_expression).grid(column=1,row=5)
outputtext=Text(mainframe)
outputtext.grid(column=1,row=5)
root.mainloop()
Screenshot:
Upvotes: 0
Reputation: 1937
For Python 3.x
After creating the entry box with inputentry=Entry(mainframe).grid()
you can get the typed entries with inputentry.get()
.
Do the following to put the typed entry in a variable and to print it in the text widget named outputtext
:
entryvar=inputentry.get() # the variable entryvar contains the text widget content
outputtext.delete(1.0,tk.END) # clear the outputtext text widget. 1.0 and tk.END are neccessary. tk implies the tkinter module. If you just want to add text dont incude that line
outputtext.insert(tk.END,entryvar) # insert the entry widget contents in the text widget. tk.END is necessary.
Upvotes: 1
Reputation: 957
3 easy steps:
1) Get the content of your variable and put it to a variable that I'm gonna name varContent
;
2) Clear your text widget, that is, if the name of your text widget is text
, then run text.delete(0, END)
;
3) Insert the string you've got in your variable varContent
into your text
text widget, that is, do text.insert(END, varContent)
.
I would do my code like this:
from tkinter import *
def check_expression():
#Your code that checks the expression
varContent = inputentry.get() # get what's written in the inputentry entry widget
outputtext.delete('0', 'end-1c') # clear the outputtext text widget
outputtext.insert(varContent)
root = Tk()
root.title("Post-fix solver")
root.geometry("500x500")
mainframe = Frame(root)
mainframe.grid(column=0, row=0)
inputentry = Entry(mainframe)
inputentry.grid(column=1, row=1)
executebutton = Button(mainframe, text="Run", command=check_expression)
executebutton.grid(column=1, row=5)
outputtext = Text(mainframe)
outputtext.grid(column=1, row=5)
root.mainloop()
Upvotes: 1
Reputation: 133919
If you're using Python 3.4+ to run the program too, you can use the contextlib.redirect_stdout
to capture the print output for a duration of a few statements into a file, or even a string:
import io
from contextlib import redirect_stdout
...
file = io.StringIO()
with redirect_stdout(file):
# here be all the commands whose print output
# we want to capture.
output = file.getvalue()
# output is a `str` whose contents is everything that was
# printed within the above with block.
Otherwise a better though a bit more arduous way is to make a StringIO
file and print to it, so you'd have
buffer = io.StringIO()
print("something", file=buffer)
print("something more", file=buffer)
output = buffer.getvalue()
text.insert(END, output)
Upvotes: 0