5

I was running a basic hex2dec converter, and wanted to transform this from console to GUI.

Now the program works fine in console, but after my conversion to GUI, it seems to throw out the int() can't convert non-string with explicit base error.

Here is the GUI code

from tkinter import *

root = Tk()
root.geometry("400x400+250+250")
root.title("Hex Converter")

heading = Label(root, text="Simple Hex to Decimal Converter", font=('arial 15 bold'), fg="steelblue").pack()

entr_hex_val = Label(root, text="Enter Hex Value to Convert", font=('arial 13 bold')).place(x=10, y=50)

my_num = IntVar()
ent_box = Entry(root, width=50, textvariable=my_num).place(x=10, y=90)

def converter():
    hexdec = my_num.get()
    dec = int(hexdec, 16)
    lab = Label(root, text=("decimal value = "+ str(dec)), font=('arial 25 bold'), fg="red").place(x=10, y=200)

conv = Button(root, text="Convert", width=12, height=2, bg="lightgreen", command=converter).place(x=10, y=130)

root.mainloop()

and the console code

import os

def hexconverter:
    os.system('cls')
    hexdec = input("Enter number in Hexadecimal Format: ")
    dec = int(hexdec, 16)
    print(str(dec))

hexconverter()

I'm struggling to see why the same code works in console but not in the GUI.

Lafexlos
  • 7,618
  • 5
  • 38
  • 53
David M
  • 53
  • 1
  • 1
  • 4
  • 1
    `my_num = IntVar()`: which likely means `my_num.get()` returns an int, not a string. –  Sep 21 '17 at 10:20
  • Thanks, i must be blind, changing that to StringVar solved that issue and the i had to remove str from + str(dec)) and now working fine. only issue from a aesthetics point of view is that the value displayed at the end in the label comes out as {Decimal value = } just wandering why the curly braces are there. but thanks for the quick response – David M Sep 21 '17 at 10:36
  • @DavidM There shouldn't be any curly brace with this given code. Maybe you tried some string formatting and put extra curly braces around? – Lafexlos Sep 21 '17 at 10:39

2 Answers2

5

The hex number needs to be a string, and you are defining my_num as an integer. Changing my_num = IntVar() to my_num = StringVar() should fix it.

SuperNano
  • 929
  • 1
  • 7
  • 9
3

When you use .get() on IntVar it returns an integer and int conversion with specified base works on strings as stated in your error message.

You can convert the value to string before using it.

dec = int(str(hexdec), 16)

But since you are using hex values, your entry might get characters A-F and IntVar would throw an error if it sees any non-integer value while using .get() so it will be easier for you to use StringVar and using try-except for catching errors on conversion.

Another point is, your code will re-create labels on each click and lab will always have the value None. Recreating might lead some memory issues (OK, maybe not in this little one but still it is worth noting). Instead of creating label everytime, you can create it once in global scope then just change its value when needed.

my_num = StringVar()  

lab = Label(root, text="", font='arial 25 bold', fg="red")
lab.place(x=10, y=200) #also notice seperated the place line to avoid NoneType error

def converter():
    hexdec = my_num.get()
    try:
        dec = int(hexdec, 16)
        lab["text"] = "decimal value = "+ str(dec)
    except ValueError:
        lab["text"] = "Error, please enter valid hex value"
Lafexlos
  • 7,618
  • 5
  • 38
  • 53