Partly to learn, partly to help myself I’m trying to write an app with a GUI for encoding/decoding. At the moment, I’m just working on encoding.
I’ve a Tkinter menu which feeds a variable to the GUI def with item specified as base64, urllib or encoding hex.
A button exists on the GUI which runs gettext. I’m having difficulty getting encodedvar to contain the process + variable and for the results to be displayed in the bottom frame.
On running this, at the moment, the following appears (as an example) in the bottom frame – blackcat obviously being what was entered into the middle frame.
base64.encodestring('blackcat
')
Have 2 issues:
-
Getting the code to actually be formatted correctly i.e. not over 2 lines as shown above
-
Have the code run, rather than the command itself be printed in the bottom.
The code I’m using is displayed below:
def gui(item):
if item == 'encode_b64':
process = 'base64.encodestring'
elif item == 'encode_url':
process = 'urllib.quote_plus'
else:
process = '.encode("hex")'
def getText():
bottomtext.delete(1.0, END)
var = middletext.get(1.0, END)
encodedvar = process + "('%s')" % var
bottomtext.insert(INSERT, encodedvar)
The text widget guarantees a trailing newline, so you should use
"end-1c"when getting the contents of the text widget. Doing that guarantees you get only the text that was entered by the user without that extra trailing newline.Second, to run the function instead of printing it out, you can store the actual function in a variable, then use the variable to invoke the function:
The above can be written a bit more succinctly like this: