-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
70 lines (57 loc) · 2.3 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import requests
import time
import tkinter as tk
from tkinter import messagebox
class CryptoPriceMonitor:
def __init__(self, root):
self.root = root
self.root.title("Crypto Price Monitor")
self.create_widgets()
self.setup_bindings()
def create_widgets(self):
self.crypto_label = tk.Label(self.root, text="Enter the name or symbol of the cryptocurrency:")
self.crypto_label.pack()
self.crypto_entry = tk.Entry(self.root)
self.crypto_entry.pack()
self.crypto_entry.focus()
self.enter_button = tk.Button(self.root, text="Enter", command=self.on_enter)
self.enter_button.pack()
self.price_label = tk.Label(self.root, text="")
self.price_label.pack()
def setup_bindings(self):
self.crypto_entry.bind("<Return>", self.on_enter)
def get_crypto_price(self, crypto):
url = f"https://api.coingecko.com/api/v3/simple/price?ids={crypto}&vs_currencies=usd"
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
crypto_lower = crypto.lower()
if crypto_lower in data:
return data[crypto_lower]["usd"]
except requests.exceptions.RequestException as e:
messagebox.showerror("Error", f"An error occurred: {e}")
return None
def update_price(self):
crypto = self.crypto_entry.get()
if not crypto:
messagebox.showwarning("Input Error", "Please enter a cryptocurrency name or symbol.")
return
price = self.get_crypto_price(crypto)
if price:
self.price_label.config(text=f"{crypto.capitalize()} Price (USD): {price}")
self.write_to_file(crypto, price)
else:
self.price_label.config(text="Invalid cryptocurrency name or symbol.")
def write_to_file(self, crypto, price):
current_time = time.strftime("%Y-%m-%d %H:%M:%S")
log_entry = f"{current_time} - {crypto}: {price}\n"
with open("crypto_logs.txt", "a") as file:
file.write(log_entry)
def on_enter(self, event=None):
self.update_price()
self.crypto_entry.delete(0, tk.END)
if __name__ == "__main__":
root = tk.Tk()
app = CryptoPriceMonitor(root)
root.mainloop()