How to convert a sum into binary How to convert a sum into binary tkinter tkinter

How to convert a sum into binary


1/ "program is supposed to convert each letter to its corresponding number position in the alphabet, add each together"You can use the list properties of string in python to perform that.

ListAlphaBet = 'abcdefghijklmnopqrstuvwxyz'Entry = 'example'number=0for i in Entry:    number += ListAlphaBet.index(i)print(number)

2/ "then convert the sum to binary"You can convert any number to a binary in python using the standard type conversion bin()

>>> a=5>>> b=bin(a)>>> b'0b101'

so to the first previous code, just add this last line:

print(bin(number))  

--------

To answer your request:

import tkinter as tkdef computeEntry():    inp = entry.get()    if inp == ' ':        return 0    ListAlphaBet = 'abcdefghijklmnopqrstuvwxyz'    number=0    for i in inp:        number += ListAlphaBet.index(i)    print(inp,number)    return numberwindow = tk.Tk()  # create the window# next create the var containing the number computed to print it afterwardvar=tk.IntVar()# next create the entry areaentry = tk.Entry(window)entry.pack()# the button command will call the set method on the var as a lambda expression,# with the var value (var.get) that will take the value computed in the function 'computeEntry'tk.Button(window, text="Compute", command=lambda:var.set(computeEntry())).pack()# the label's textvariable is set to the value of 'var'tk.Label(window, textvariable=var).pack()tk.mainloop() # main loop of the window

You now have all the elements answering your questions with full comments, you only have to put this code into object-oriented code if you really want to.


You do actually have two problems:

  • Compute binary sum of letters
  • Integrate this into your GUI

Sol answered the first problem. Here are some insights for the second one:

class NameGUI:    def __init__(self):        # Some code        self.ok_button = tk.Button(self.bottom_frame, text = 'OK', command = self.command_ok)        # Some more code    def command_ok(self):        first_name = self.fname_entry.get()        last_name = self.lname_entry.get()        # Do whatever you want        self.value.set(first_name + "-" + last_name)

I do not give an extensive solution, I let you try my code chunk and combine it with sol's answer. In particular, you might want to call his function computeEntry once or twice somewhere within command_ok. Also, you will probably need to have different variables for self.name1_label and self.name2_label.