Hello! It is impossible to display the return function in the lable tk.

import requests from bs4 import BeautifulSoup import tkinter as tk from tkinter import ttk app = tk.Tk() global ans global kurs2 ans = '' k1_label = ttk.Label(app, text='Валюта 1') k1_label.grid(row=0, column=0) k1_text = ttk.Entry(app, width=10) k1_text.insert(0, 'btc') k1_text.grid(row=0, column=1) k2_label = ttk.Label(app, text='Валюта 2') k2_label.grid(row=1, column=0) k2_text = ttk.Entry(app, width=10) k2_text.insert(0, 'usd') k2_text.grid(row=1, column=1) def curs_php(): url = 'https://www.calc.ru/kurs-PHP-RUB.html' page = requests.get(url) r = page.text soup = BeautifulSoup(r, 'lxml') get_kurs = soup.find_all('div', class_="t18")[1].select('strong')[0] kurs = str(get_kurs).split(' ')[3] return float(kurs) def get_etc(): x = k1_text.get() y = k2_text.get() if y == 'php': try: url = 'https://api.cryptonator.com/api/ticker/{}-rur'.format(x) response = requests.get(url).json() price = response['ticker']['price'] p = float(price) g = p/kurs2 c = round(g, 3) ans = x.upper() + ' ' + str(c)+ ' ' + y.upper() except: return 'no data' else: try: url = 'https://api.cryptonator.com/api/ticker/{}-{}'.format(x, y) response = requests.get(url).json() price = response['ticker']['price'] p = float(price) c = round(p, 3) ans = x.upper() + ' ' + str(c)+ ' ' + y.upper() except: return 'no data' k3_label = ttk.Label(app, text=ans) k3_label.grid(row=3, column=0) btn_gen = ttk.Button(app, text='Запросить', width=15, command=get_etc) btn_gen.grid(row=2, column=1) print(ans) kurs2 = curs_php() curs_php() app.mainloop() 

thank you in advance!

  • And what does this script do? - Alexander
  • for a pair of currency names, for example, btc-rur returns the btc price in rubles. - Ginjou

1 answer 1

There will be questions write

 import requests from tkinter import Tk, mainloop from tkinter.ttk import Label, Entry, Button URL = 'https://api.cryptonator.com/api/ticker/{0}' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.2)\ AppleWebKit/537.36 (KHTML, like Gecko)\ Chrome/63.0.3239.84 Safari/537.36', 'Accept-Language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7'} class App(Tk): price = round def __init__(self): super().__init__() self.title('Курс валюты') self.interface() def interface(self): title = Label(self, text='Введите валютную пару в формате xxx-xxx') title.config(font=('Times', 12)) title.grid(row=0, column=0) self.currency_pair = Entry(self, width=10) self.currency_pair.grid(row=0, column=1, ipadx=4) self.showing_price = Label(self) self.showing_price.config(font=('Times', 12)) self.showing_price.grid(row=1, column=0) self.button_run = Button(self, text='запросить') self.button_run.config(command=self.returning_exchange_rate) self.button_run.grid(row=1, column=1) def returning_exchange_rate(self): print(self.currency_pair.get()) response = requests.get(URL.format(self.currency_pair.get()), headers=headers).json() data = float(response['ticker']['price']) self.price = round(data, 4) self.showing_price['text'] = str(self.price) def main(): app = App() app.mainloop() if __name__ == '__main__': main() 
  • Thank you very much! and what does the super function do? - Ginjou
  • The class App inherits the class Tk and here this line super () .__ init __ () allows you to access any method and attribute Tk through the class App, which the next line self.title ('Currency rate') proves perfectly well Tk .__ init __ () - Alexander
  • Did the answer to the question help you? So accept it ;-) And by clicking on the up arrow you will thus sympathize with the answer, and vice versa. - Alexander