Is it possible to use **> operators** in Tkinter

So, basically, I’m attempting to write a Python programme that will determine whether or not the three-digit number entered by the user is an Armstrong number. It works fine in Python, except that it prints the answer three times in the terminal. But when I use tkinter, I have some issues because I only learned about it a few hours ago and don’t really know how to use it. The issue I’m having is with the > operator; I’m receiving data in Entry(), but the > operator is only for integers, so I’m getting type errors.

import re
from tkinter import *

root = Tk()

label = Label(root, text="Enter a 3 digit Number")
label.pack()

num = Entry(root, width=50, bg="orangered")
num.pack()

def myclick():
    Sum = 0
    temp = num

    if 1000 > num > 99:
        while temp > 0:
            digit = temp % 10
            Sum += digit ** 3
            temp //= 10
            if num == Sum:
                label2 = Label(root, num, "is an Armstrong number")
                label2.pack()
            else:
                label3 = Label(root, num, "is not an Armstrong number")
                label3.pack()
    else:
        label4 = Label(root,
                       "The number you entered is not a 3 digit number, Please Enter a number between 100 and 999")
        label4.pack()

MyButton = Button(root, text="Click", command=myclick)
MyButton.pack()
root.mainloop()

Error: TypeError: '>' not supported between instances of 'int' and 'Entry'
All I want is for this programme to run in a graphical environment and to stop printing the result three times. Can anyone assist me?

Hey Bongshruti ,

This is pretty cool code! One of the problems was on line 14. You have to cast temp into an integer.

temp = int(num.get())

I corrected a few other logical errors to make the code work as expected.

Complete code

import tkinter as tk

root = tk.Tk()
label = tk.Label(root, text="Enter a 3 digit Number")
label.pack()

num = tk.Entry(root, width=50, bg="orangered")
num.pack()


def my_click():
    number = int(num.get())
    temp = number
    total = 0
    if 1000 > number > 99:
        while temp != 0:
            digit = temp % 10
            total += digit ** 3
            temp = temp // 10
        if number == total:
            label2 = tk.Label(root, text=f"{number} is an Armstrong number")
            label2.pack()
        else:
            label3 = tk.Label(root, text=f"{number} is not an Armstrong number")
            label3.pack()
    else:
        label4 = tk.Label(root,
                          text="The number you entered is not a 3 digit number, Please Enter a number between 100 and 999")
        label4.pack()


if __name__ == '__main__':
    MyButton = tk.Button(root, text="Click", command=my_click)
    MyButton.pack()
    root.mainloop()

Screen Shot

image

Note:" A single digit number was given on the first entry. 100 is not an Armstrong number, and 153 is.

FYI

Using import * in python programs is considered a bad habit because you are polluting the namespace, the import * statement imports all the functions and classes into your own namespace, which may clash with the functions you define or functions of other libraries that you import. You should consider importing tkinter with the following syntax:

import tkinter as tk

This way it can be used to create an instance of widgets if needed. Let me know if this resolves your problem!

Best regards,

1 Like