Python 3 Tkinter Encode Text or String to Binary Code Converter GUI Desktop App Using Regular Expression Library Full Project For Beginners

You are currently viewing Python 3 Tkinter Encode Text or String to Binary Code Converter GUI Desktop App Using Regular Expression Library Full Project For Beginners

 

Welcome folks today in this post we will be building text to binary code gui desktop app using regular expression library in tkinter. All the full source code of the application is given below.

 

 

Get Started

 

 

In order to get started you need to make an app.py file and copy paste the following code

 

app.py

 

#!/usr/bin/env python

__author__ = 'Max DeLiso'

import tkinter
import tkinter.constants

from binutils import binEncodeString, attemptDecode, binDecodeString

class TkTest(tkinter.Frame):
  def __init__(self, master=None):
    tkinter.Frame.__init__(self, master)

    #TODO: refactor these into a class for brevity

    self.INPUT_L = tkinter.Text(self)
    self.INPUT_L.grid(column=0, row=1, sticky=tkinter.NSEW, padx=2, pady=2)
    self.INPUT_L.bind("<KeyRelease>", self.on_left_text_update)
    self.INPUT_L.bind("<FocusIn>", self.on_left_text_update)

    self.INPUT_R = tkinter.Text(self)
    self.INPUT_R.grid(column=1, row=1, sticky=tkinter.NSEW, padx=2, pady=2)
    self.INPUT_R.bind("<KeyRelease>", self.on_right_text_update)
    self.INPUT_R.bind("<FocusIn>", self.on_right_text_update)

    self.LABEL_TOP = tkinter.Label(
      self,
      text="text in left field, binary strings in the right",
      justify=tkinter.constants.CENTER)
    self.LABEL_TOP.grid(row=0, columnspan=2, sticky=tkinter.S, pady=4)

    self.grid_columnconfigure(0, weight=1)
    self.grid_columnconfigure(1, weight=1)
    self.grid_rowconfigure(0, minsize=16)
    self.grid_rowconfigure(1, weight=1)

  def on_left_text_update(self, ev):
    self.INPUT_R.replace("0.0", tkinter.END,
      binEncodeString(self.INPUT_L.get("0.0", tkinter.END)))

  def on_right_text_update(self, ev): # TODO: do this line by line with insert/delete
    self.INPUT_L.replace("0.0", tkinter.END,
      binDecodeString(self.INPUT_R.get("0.0", tkinter.END)))

if __name__ == "__main__":
  root = tkinter.Tk()

  app = TkTest(root)
  app.grid(sticky=tkinter.NSEW)

  root.title("bin2text")
  root.grid_columnconfigure(0, weight=1)
  root.grid_rowconfigure(0, weight=1)
  root.resizable(True, True)

  root.mainloop()

 

 

Now make a binutils.py file and copy paste the following code

 

binutils.py

 

import re

def binEncodeString(rawString):
  return ' '.join(
    map(lambda ch: str(bin(ord(ch))[2:]),
      rawString))

def attemptDecode(numStr):
  try:
    return str(chr(int(numStr, base=2)))
  except ValueError:
    pass
  except UnicodeEncodeError:
    pass

def binDecodeString(binStr):
  return ''.join(
    filter(None,
      list(map(attemptDecode,
        re.findall(r'[0-1]+', binStr)))))

 

 

Now run the python script by typing the below command as shown below

 

python app.py

 

Leave a Reply