Friday, September 15, 2023

New_App

 Finally got working code for a tkinter to Hex conversion. Below,

on a minimalist interface:

                                                


The code:                                                              


import tkinter as tk
from tkinter import*
root = Tk()
root.title('Hex Conversion')
root.geometry("600x400")
root.configure(bg='#97AAB8')

# Separate entries for rgb.

def get_rgb():
r = my_entry_r.get()
g = my_entry_g.get()
b = my_entry_b.get()
rgb = (r, g, b)

my_tuple = list(map(int, rgb))
my_hex = '#{:02x}{:02x}{:02x}'.format(*my_tuple)

my_res.config(text=my_hex)

# Resets the interface.
def clear_entries():
for widget in root.winfo_children():
if isinstance(widget, tk.Entry):
widget.delete(0, tk.END)
my_res.config(text=" ")
my_entry_r.focus()

# The top label.
my_L1 = Label(root, text='Convert from rgb to Hex', font=('Arial', 10, 'normal'))
my_L1.place(x=50, y=50)

# The entry labels for the individual channels.
my_entry_r = tk.Entry(root, width=10, font=('Arial', 10, 'normal'))
my_entry_r.place(x=50, y=150)
my_entry_g = tk.Entry(root, width=10, font=('Arial', 10, 'normal'))
my_entry_g.place(x=150, y=150)
my_entry_b = tk.Entry(root, width=10, font=('Arial', 10, 'normal'))
my_entry_b.place(x=250, y=150)

# The two buttons.
sub_btn=tk.Button(root, text='Convert', command=get_rgb)
sub_btn.place(x=50, y=250)

reset_btn = tk.Button(root, text="RESET", command=clear_entries)
reset_btn.place(x=200, y=250)

my_res = tk.Label(root, width=10, text="", font=('Arial', 10, 'normal'))
my_res.place(x=50, y=350)

# Keeping the window open...
root.mainloop() 

One function I had to research, and ended up finding on Stack Overflow, is the map() function.

I needed to change the elements in my rgb to integers, so that the hex conversion would work.

Below:
                                                              

rgb works fine f one feeds ints into it:

                                                                      


My tkinter interface was feeding in strings, making the map() function necessary:
                                  
                                                                       


There is no getting around that the get() from the entry boxes returns strings.

One would need to convert them immediately to ints, and proceed with the hex

conversion.

                                                                      


                                                                            




No comments: