0

I am trying to construct a Roman numeral converter using Tkinter. I need to use one button which will convert decimals to Roman numerals if numbers are entered, and Roman numerals to decimals if numerals are entered. How do I assign the two functions to one button, and call whichever function corresponds to what is entered?

This is what I have so far:

    from tkinter import *
    root = Tk()
    root.title('Roman numerals')
    root.geometry('400x300')

    numRomanMap = (('M',  1000), ('CM', 900), ('D',  500), ('CD', 400), ('C',  100), ('XC', 90), ('L',  50), ('XL', 40), ('X',  10), ('IX', 9), ('V',  5), ('IV', 4), ('I',  1))

    def roman_number():
        roman_string = ''
        n = float(eval(ent.get()))
        wrongValue = ('Number is out of range')
        if not 1 <= n <= 9999:
                    return wrongValue
        for numeral, integer in numRomanMap:
            while n >= integer:
                roman_string += numeral
                n -= integer
        print (roman_string)

    def decimal_number(s):
        dec_number = 0
        s = str(eval(ent.get()))
        index = []
        for i in range(len(s)):
            for numeral, integer in numRomanMap:
                if s[i] == numeral:
                    index.append(integer)
        index.append(0)
        for i in range(len(s)):
            if index[i] >= index[i+1]:
                dec_number = dec_number + index[i]
            else:
                dec_number = dec_number - index[i]
        print (dec_number)

    Convertb = Button(root, text='Convert', command = roman_number)
    ent = Entry(root)
    ent.pack()
    ent.delete(0, END)
    ent.insert(0, 'Enter a number')
    Convertb.pack()

    root.mainloop()
1
  • 3
    could make a function which simply checks the input of the user, and then that branches off into two other functions, one for decimal input and the other for RN input Commented Oct 20, 2017 at 10:08

1 Answer 1

1

Add a function to see if the input is a number.

def check_input():
    s = ent.get()
    if (isinstance(s, int)):
        roman_number(s)
    else:
        decimal_number(s)

And call that function from the button:

Convertb = Button(root, text='Convert', command = check_input)
Sign up to request clarification or add additional context in comments.

5 Comments

Does tkinters entry.get() return int if its get is only int? or does it always, no matter the input, grab the input as a str?
@Goralight entry.get() always returns a string, so you can use something like s.isdigit() to check the input.
@Avión Thanks for your answer. I'm receiving the following error messages: line 40, in check_input decimal_number(n) line 29, in decimal_number if index[i] >= index[i+1]: IndexError: list index out of range
@Greta That's a problem with your decimal_number function, not with the question you stated on the post.
Glad No problem, @Greta . If it helped, please, tick the answer. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.