0

I have a python script that can convert binary to decimal and decimal to binary, and I would like to be able to highlight text, right click it, select an option in the drop down menu on windows (10) and have it convert the number, telling me in a notification or something. I know that in order to run the script I will need to edit the registry, but exactly how, and how I can use the highlighted text as input and send a notification is a mystery to me. If it is of any help whatsoever, the code currently is

    import math

def binToDec(binary):
    decimal = 0
    i = 0
    while (binary != 0):
        remainder = binary%10
        binary = int(binary/10)
        decimal += remainder*pow(2,i)
        i+=1
    return int(decimal)

def decToBin(decimal):
    binary = ""
    while (decimal > 0):
        remainder = int(decimal%2)
        div = int(decimal/2)
        binary += str(remainder)
        decimal = div
    return int(binary[::-1])

which I know is useless in this state. I generally need a point in the right direction or instructions on how I would go about this, it would be much appreciated.

1 Answers1

0

You can use GUI libraries python offers.

With regard to this question, Here is a piece code that creates a menubutton list with options of binToDec(1010) and decToBin(10) of your example, using tkinter(python3x):

import math

def binToDec(binary):
    decimal = 0
    i = 0
    while (binary != 0):
        remainder = binary%10
        binary = int(binary/10)
        decimal += remainder*pow(2,i)
        i+=1
    return int(decimal)

def decToBin(decimal):
    binary = ""
    while (decimal > 0):
        remainder = int(decimal%2)
        div = int(decimal/2)
        binary += str(remainder)
        decimal = div
    return int(binary[::-1])



import tkinter as tk

def cbc(num, tex):
    return lambda : callback(num, tex)

def callback(num, tex):
    s = '{}\n'.format(num)
    tex.insert(tk.END, s)
    tex.see(tk.END)        

top = tk.Tk()
tex = tk.Text(master=top)
tex.pack(side=tk.RIGHT)
bop = tk.Frame()
bop.pack(side=tk.LEFT)

b1 = tk.Button(bop, text="binToDec(1010)",
       command=cbc(binToDec(1010),tex))
b1.pack()
b2 = tk.Button(bop, text="decToBin(10)", command=cbc(decToBin(10),tex))
b2.pack()

tk.Button(bop, text='Exit', command=top.destroy).pack()
top.mainloop()

Though it would be, for your script, much simpler to just make it a command line utility(install python in your windows environment then run your script directly from command line(cmd)).

Community
  • 1
  • 1
Leo Mingo
  • 40
  • 8
  • How would I run the script via cmd? – BlackSlash The Dragon May 05 '17 at 19:14
  • When you type a command in cmd(such as "notepad", "dir"), you are actually executing a program under C:\Windows\System32\ , assuming the root drive is C:. So you can put python.exe under that location, then go to the path of your script file, type "python filename.py", or you can make your script file an executable, consider this [introduction](https://mborgerson.com/creating-an-executable-from-a-python-script) – Leo Mingo May 06 '17 at 01:54