Exciting Python tkinter Program #shorts

Posted by

Python tkinter Very Interesting Program #shorts

Python tkinter Very Interesting Program #shorts

Python is a popular programming language that is used for a wide range of applications, from web development to data analysis. One of the key strengths of Python is its extensive library support, which includes the tkinter library for creating graphical user interfaces (GUIs).

With tkinter, developers can create interactive programs with buttons, labels, text boxes, and more, all with a few lines of code. In this article, we will explore a very interesting program created using Python tkinter.

The Program

Our program is a simple calculator that takes two numbers as input from the user, performs a calculation (addition, subtraction, multiplication, or division), and displays the result on the screen. The user can select the type of operation to perform using radio buttons.

Here is the code for our program:

import tkinter as tk

def calculate():
    num1 = float(entry1.get())
    num2 = float(entry2.get())
    
    if operation.get() == 1:
        result.set(num1 + num2)
    elif operation.get() == 2:
        result.set(num1 - num2)
    elif operation.get() == 3:
        result.set(num1 * num2)
    elif operation.get() == 4:
        result.set(num1 / num2)

root = tk.Tk()
root.title("Simple Calculator")

entry1 = tk.Entry(root)
entry1.pack()

entry2 = tk.Entry(root)
entry2.pack()

operation = tk.IntVar()
tk.Radiobutton(root, text="Addition", variable=operation, value=1).pack()
tk.Radiobutton(root, text="Subtraction", variable=operation, value=2).pack()
tk.Radiobutton(root, text="Multiplication", variable=operation, value=3).pack()
tk.Radiobutton(root, text="Division", variable=operation, value=4).pack()

result = tk.StringVar()
tk.Label(root, textvariable=result).pack()

calculate_button = tk.Button(root, text="Calculate", command=calculate)
calculate_button.pack()

root.mainloop()

Conclusion

Python tkinter is a versatile library that allows developers to create engaging and interactive GUIs for their programs. With just a few lines of code, you can create complex applications that are easy for users to navigate and use.

Our simple calculator program is just one example of what you can create with tkinter. We encourage you to explore the library further and see what other interesting programs you can develop.

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@smartunboxing5716
3 months ago

Nice video