AzGames
AzGames

Reputation: 13

Updating Label text tkinter

from tkinter import *
import tkinter as tk

def text_to_binary(convert):
    for x in convert: # iterates over each character in the list
        ascii_values = ord(x) #gets the ascii value for x
        binary = format(ascii_values,'b')
        if len(binary) == 7:
            tk.Label(text="'0',binary")
            tk.Label.pack()
        elif len(binary) == 6:
            tk.Label(text="'00',binary")
            tk.Label.pack()
        else:
            tk.Label(text=binary)
            tk.Label.pack()

###MAINLINE###
window = tk.Tk()
window.title('Binary Converter')
window.geometry("450x120")

entry = tk.Entry(width=50)
entry.pack()

result = entry.get()

btn_convert = tk.Button(text='Convert it!', command=text_to_binary(result))
btn_convert.pack()

window.mainloop()

I'm not sure what I'm doing wrong. I want to use the function I created to convert bianry, it does that, but I don't know how to get that running with tkinter gui. Little help?

Upvotes: 2

Views: 111

Answers (2)

toyota Supra
toyota Supra

Reputation: 4537

There are three ways to convert from string to binary.

Put the Entry widget outside of the function and use configure inside the function. It is critically widely practical.

The command when using Method join() + ord() + format().

import tkinter as tk


def text_to_binary()->None:
    test_str = entry.get()
    result = ''.join(format(ord(i), '08b') for i in test_str)))
    lbl_binary.configure(text=result)
            
         
###MAINLINE###
window = tk.Tk()
window.title('Binary Converter')
window.geometry("450x120")

entry = tk.Entry(width=50)
entry.pack()

result = entry.get()

lbl_binary = tk.Label(window)
lbl_binary.pack()

btn_convert = tk.Button(text='Convert it!', command=text_to_binary)
btn_convert.pack()

window.mainloop()

The command when using Method join() + bytearray() + format()

import tkinter as tk 

def text_to_binary()->None:
    test_str = entry.get()
    result = ''.join(format(i, '08b') for i in bytearray(test_str, encoding ='utf-8'))
    lbl_binary.configure(text=result)
            
         
###MAINLINE###
window = tk.Tk()
window.title('Binary Converter')
window.geometry("450x120")

entry = tk.Entry(width=50)
entry.pack()

result = entry.get()

lbl_binary = tk.Label(window)
lbl_binary.pack()

btn_convert = tk.Button(text='Convert it!', command=text_to_binary)
btn_convert.pack()

window.mainloop()

Using an empty list to store binary values.

import tkinter as tk

def text_to_binary()-> None:
    test_str = entry.get()
    result = [bin(ord(char))[2:].zfill(8)for char in test_str]
    lbl_binary.configure(text=result)
     
                     
###MAINLINE###
window = tk.Tk()
window.title('Binary Converter')
window.geometry("500x120")

entry = tk.Entry(width=50)
entry.pack()

lbl_binary = tk.Label(window)
lbl_binary.pack()

btn_convert = tk.Button(text='Convert it!', command=text_to_binary)
btn_convert.pack()

window.mainloop()

Screenshot:

enter image description here

Upvotes: 0

acw1668
acw1668

Reputation: 46669

Below are the issues in your code:

  • both from tkinter import * and import tkinter as tk are executed. Wildcard import is not recommended, so remove from tkinter import *;
  • calling result = entry.get() right after entry is created, so result will be empty string;
  • command=text_to_binary(result) will execute text_to_binary(result) immediately without clicking the button. It should be command=lambda: text_to_binary(entry.get()) instead;
  • tk.Label.pack() will raise exception. .pack() should be called on instance of tk.Label();

Below is the modified code:

import tkinter as tk

def text_to_binary(convert):
    # clear existing result
    for w in result.winfo_children():
        w.destroy()

    for x in convert: # iterates over each character in the list
        ascii_values = ord(x) #gets the ascii value for x
        binary = format(ascii_values, '08b')
        tk.Label(result, text=binary).pack()

###MAINLINE###
window = tk.Tk()
window.title('Binary Converter')
#window.geometry("450x120")

entry = tk.Entry(window, width=50)
entry.pack()

btn_convert = tk.Button(window, text='Convert it!', command=lambda: text_to_binary(entry.get()))
btn_convert.pack()

# frame for the conversion result
result = tk.Frame(window)
result.pack()

window.mainloop()

Upvotes: 2

Related Questions