Tkinter

Python Program to check Armstrong Number using Tkinter4 min read

An Armstrong number (also known as a narcissistic number or a pluperfect digital invariant) is a number that is equal to the sum of its own digits raised to the power of the number of digits.

For example, the number 153 is an Armstrong number because:




1^3 + 5^3 + 3^3 = 153

Similarly, the number 371 is an Armstrong number because:

3^3 + 7^3 + 1^3 = 371

And the number 9474 is an Armstrong number because:

9^4 + 4^4 + 7^4 + 4^4 = 9474

So, in general, if a number n has d digits, it is an Armstrong number if the sum of each digit raised to the power d is equal to n.

Python Program to check Armstrong Number using Tkinter
Python Program to check Armstrong Number using Tkinter

In this program, a GUI is created using the Tkinter library to allow the user to enter a number and check if it is an Armstrong number. The check_armstrong function performs the calculation to determine if the number is an Armstrong number and displays the result using a messagebox from Tkinter.

Here is an explanation of the code, line by line:

This imports the Tkinter library and the messagebox module, which is used to display messages in a pop-up window.

This defines the check_armstrong function, which is called when the user clicks the “Check” button. The function starts by converting the string entered in the text entry field to an integer and storing it in the number variable.

The sum variable is used to store the sum of the digits raised to the power of the number of digits in the input number. temp is a variable that is used to store the original number for processing. length stores the number of digits in the input number.

This is the loop that performs the calculation to determine if the number is an Armstrong number. The loop continues as long as temp is greater than 0. For each iteration of the loop, the last digit of temp is extracted by taking the remainder of temp divided by 10, and stored in the digit variable. Then, digit is raised to the power of length and added to the sum. Finally, temp is updated by integer division by 10 to remove the last digit.

After the loop has completed, the function checks if the number is equal to sum. If they are equal, the number is an Armstrong number, and a message box is displayed to indicate this. If they are not equal, the number is not an Armstrong number, and a message box is displayed to indicate this.

This creates a Tkinter window object and sets its title to “Armstrong Number Checker”.

These three lines create the label, text entry, and button widgets that make up the user interface. The label displays the text “Enter a number:”, the entry allows the user to enter a number, and the button allows the user to check if the entered number is an Armstrong number.

These lines arrange the widgets in the window using the pack geometry manager.

This starts the Tkinter event loop, which displays the window and listens for user events (such as clicking the button). The program will continue to run until the window is closed.

Leave a Comment