Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to change the text in label? ;/
#1
hi
import requests
import tkinter as tk

currencies = ['EUR', 'USD', 'CHF', 'GBP']

okno = tk.Tk()
okno.title("Kurs walut")
okno.geometry("200x200")

def waluty():
    for currency in currencies:
        r = requests.get('https://user.walutomat.pl/api/public/marketBrief/{}_PLN'.format(currency))
        data = r.json()
        fx_rate = data['bestOffers']['ask_now']
        print('{} = {} PLN'.format(currency, fx_rate))
        label = tk.Label(okno, text='{} = {} PLN'.format(currency, fx_rate))
        label.pack()

waluty()
refresh = tk.Button(okno, text="Refresh", command=waluty)

refresh.pack()
okno.mainloop()
When i want to refresh data, then create another label below button. I dont know how to change date in the first label. I was trying using something like stringVar(), label.config etc but it doesnt working for me ;(
I think its hard becouse there are 4 labels after click button and i need to update all 4 labels or do it in other way.
I've also tried to create list with 4 element and display her after funcktion but failed.

Python look much harder than C++ for me. In C++ i did a lot of execises from books and websites, but in python i cant even do so simple execise wtf
Quote
#2
Instead of text= use textvariable= and the Label is updated, when you update the StringVar.

import tkinter as tk

 
okno = tk.Tk()
text_var = tk.StringVar(okno)
tk.Label(okno, textvariable=text_var).pack()
# a textvariable is used.
# remind, if you need the reference to Label,
# you need two line. The method pack returns none.

# setting now the text
text_var.set('Abc123')

# to get the content of the variable_
print(text_var.get())

okno.mainloop()
I guess you want to make more than one label. For each label you've to create an own StingVar.
You can put this variables for example in a dict.
Afterwards you have to update each StringVar in the function with the set method.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote
#3
Yeah, i know that, but i dont know how to implement this in my code.
Quote
#4
Check this out. Only create the labels one time, at initialization, and bind stringvars to them. Then, when you refresh, the only thing you do is update those stringvars. Which means you need to keep them somewhere you can get them later, so I changed your currencies into a dict, with the values of the dict being the stringvar for a particular currency.

import requests
import tkinter as tk
 
okno = tk.Tk()
okno.title("Kurs walut")
okno.geometry("200x200")

# a different stringvar for each value
currency_labels = {
    'EUR': tk.StringVar(okno),
    'USD': tk.StringVar(okno),
    'CHF': tk.StringVar(okno),
    'GBP': tk.StringVar(okno)
}

# create the labels, and add them to the panel
for currency in currency_labels:
    label = tk.Label(okno, textvariable=currency_labels[currency])
    label.pack()

def waluty():
    for currency in currency_labels:
        r = requests.get('https://user.walutomat.pl/api/public/marketBrief/{}_PLN'.format(currency))
        data = r.json()
        fx_rate = data['bestOffers']['ask_now']
        
        text = '{} = {} PLN'.format(currency, fx_rate)
        print(text)
        # instead of creating a new label, we update the contents of the variable the current label already uses
        currency_labels[currency].set(text)
        
waluty()
refresh = tk.Button(okno, text="Refresh", command=waluty)
 
refresh.pack()
okno.mainloop()
(Jul-18-2018, 03:02 PM)Dzonzi Wrote: Python look much harder than C++ for me. In C++ i did a lot of execises from books and websites, but in python i cant even do so simple execise wtf
Anything new requires an adjustment period, especially when that new thing requires a different way of thinking. That said, knowing C++ definitely helps :)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [Tkinter] Change Label Every 5 Seconds gw1500se 4 99 May-26-2020, 05:32 PM
Last Post: gw1500se
  [Tkinter] Python 3 change label text gw1500se 6 699 May-08-2020, 05:47 PM
Last Post: deanhystad
  [Kivy] Kivy text label won't shows up! AVD_01 0 250 May-02-2020, 07:11 AM
Last Post: AVD_01
  [Tkinter] Change label for multiple frames Dandy_Don 3 239 Apr-30-2020, 02:22 PM
Last Post: Dandy_Don
  [Tkinter] how to update label text from list Roshan 8 245 Apr-25-2020, 08:04 AM
Last Post: Roshan
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 497 Mar-20-2020, 07:21 AM
Last Post: deanhystad
  [Tkinter] Label, align imported text from pandas kundrius 2 511 Dec-11-2019, 08:26 AM
Last Post: kundrius
  Make Label Text background (default color) transparent using tkinter in python barry76 1 5,590 Nov-28-2019, 10:19 AM
Last Post: Larz60+
  Update a label text from subprocess jim53 3 1,013 Aug-19-2019, 08:21 PM
Last Post: Denni
  Unable to update or refresh label text in tkinter jenkins43 3 1,958 Jul-24-2019, 02:09 PM
Last Post: Friend

Forum Jump:


Users browsing this thread: 2 Guest(s)