[Tutor] Fwd: Re: Dynamic variables changed via Tkinter button

Alan Gauld alan.gauld at yahoo.co.uk
Sun Apr 19 10:21:40 EDT 2020


On 19/04/2020 15:11, Alan Gauld via Tutor wrote:

> I suppose my only question now, to seal the deal, is how can I display
> the selected currency symbol as part of the price label in the Fuelling
> page? While I can verify that the currency selection/conversion is
> working through looking at the actual numbers that are presented,
> showing the actual currency symbol would make it a lot more evident.
> 
> I'd assumed it would be something like the below (having taken
> inspiration from the def MakeCurrencyButton above)
> 
> 
>         self.price_lbl = tk.Label(self, text = _(f"{symbol}" + "Price = 0"))
>         self.price_lbl.pack(pady = 10, padx = 10)
> 
>     def update(self, symbol):                                           
>         self.fuelcount += 0.1
>         total_price = self.fuelcount * self.controller.currency * PETROL_PRICE
>         self.litre_lbl['text'] = _("Litres = %f") % self.fuelcount
>         self.price_lbl['text'] = _(f"{symbol}" + "Price = %f") % total_price

Yes, it is just a matter of using string formatting. (You don't
need to worry about internationalization/gettext issues because
the currency symbol is the same regardless of language).
So you don't need the _() around the symbol - and indeed the symbol
would require you to duplicate the translation for each currency symbol!

However, in the line above you use two different formatting mechanisms
in the same string. While that is allowed and works it is better for
consistency to use a single style.
Since you seem to have Pyhthon 3.8 format strings is probably the best
option:

self.price_lbl['text'] = f"{symbol}" + _("Price = ") + f"{total_price}")

Note: Only the literal string "Price = " needs to have the _()
gettext marker.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list