0% found this document useful (0 votes)
20 views3 pages

Jihzn

This Python code defines functions for a basic calculator GUI. It creates Tkinter widgets like buttons and entries to allow users to enter numbers, perform operations, and view results. Functions are defined to handle button clicks and insert text, evaluate expressions, convert values to binary, plot graphs, and more. The widgets are laid out in a grid and the GUI is run in a mainloop.

Uploaded by

djihenebenbouha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Jihzn

This Python code defines functions for a basic calculator GUI. It creates Tkinter widgets like buttons and entries to allow users to enter numbers, perform operations, and view results. Functions are defined to handle button clicks and insert text, evaluate expressions, convert values to binary, plot graphs, and more. The widgets are laid out in a grid and the GUI is run in a mainloop.

Uploaded by

djihenebenbouha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

from tkinter import *

from tkinter import ttk


import math
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

def insert(text):
if text != "=":
En.insert("end", text)
else:
try:
operation = eval(En.get())
En.delete(0, "end")
En.insert(0, "{}".format(operation))
except Exception as e:
En.delete(0, "end")
En.insert(0, "Erreur")

def convert_to_binary():
current_text = En.get()
try:
decimal_value = eval(current_text)
binary_value = bin(decimal_value)[2:] # [2:] to remove the '0b' prefix
from the binary representation
En.delete(0, "end")
En.insert(0, "{} (binaire)".format(binary_value))
except Exception as e:
En.delete(0, "end")
En.insert(0, "Erreur")

def button(text, clmn, rw):


ttk.Button(root, text=text, command=lambda: insert(text)).grid(
column=clmn, row=rw, ipadx=15, ipady=15, sticky="nsew")

def power():
current_text = En.get()
En.delete(0, "end")
En.insert(0, "{}**2".format(current_text))

def exponential():
current_text = En.get()
En.delete(0, "end")
En.insert(0, "math.exp({})".format(current_text))

#def calculate_delta():
# try:
# current_text = En.get()
# a, b, c = [float(x) for x in current_text.split()]
#delta = b**2 - 4*a*c
# En.delete(0, "end")
# En.insert(0, "Δ = {}".format(delta))
#except Exception as e:
# En.delete(0, "end")
# En.insert(0, "Erreur")

def plot_graph():
try:
current_text = En.get()
x = range(-10, 11)
y = [eval(current_text.replace("x", str(i))) for i in x]

fig, ax = plt.subplots()
ax.plot(x, y)
ax.set(xlabel='x', ylabel='y',
title='Graph')
ax.grid()

canvas = FigureCanvasTkAgg(fig, master=root)


canvas_widget = canvas.get_tk_widget()
canvas_widget.grid(column=0, row=9, columnspan=4, ipadx=10, ipady=10,
sticky="nsew")

except Exception as e:
En.delete(0, "end")
En.insert(0, "Erreur")

# Configure the style


style = ttk.Style()
style.configure("TButton", font=("Arial", 12), padding=5)

root = Tk()
root.title("Calculatrice")
root.geometry("600x800")
root.configure(bg="#F2F2F2")

En = ttk.Entry(root, font="Arial 15", justify="right")


En.grid(column=0, row=0, columnspan=4, ipadx=15, ipady=15, sticky="nsew")

# Configure grid weights


for i in range(1, 5):
root.grid_columnconfigure(i, weight=1)
root.grid_rowconfigure(i, weight=1)

# Button layout and styling


button("1", 1, 1)
button("2", 2, 1)
button("3", 3, 1)

button("4", 1, 2)
button("5", 2, 2)
button("6", 3, 2)

button("7", 1, 3)
button("8", 2, 3)
button("9", 3, 3)

button("+", 1, 4)
button("-", 2, 4)
button("*", 3, 4)

button("/", 1, 5)
button("0", 2, 5)
button(".", 3, 5)

ttk.Button(root, text="=", command=lambda: insert("=")).grid(column=4, row=2,


rowspan=2, ipadx=15, ipady=45, sticky="nsew")
ttk.Button(root, text="x^2", command=power).grid(column=4, row=1, ipadx=15,
ipady=15, sticky="nsew")
ttk.Button(root, text="exp", command=exponential).grid(column=4, row=3, ipadx=15,
ipady=15, sticky="nsew")
ttk.Button(root, text="Convertir en binaire",
command=convert_to_binary).grid(column=1, row=6, columnspan=3, ipadx=15, ipady=15,
sticky="nsew")
ttk.Button(root, text="Plot Graph", command=plot_graph).grid(column=1, row=7,
columnspan=3, ipadx=15, ipady=15, sticky="nsew")
#ttk.Button(root, text="Δ (Delta)", command=calculate_delta).grid(column=1, row=8,
columnspan=3, ipadx=15, ipady=15, sticky="nsew")

root.mainloop()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy