[Tutor] Fwd: Re: Dynamic variables changed via Tkinter button
Jon Davies
jon_davies17 at hotmail.co.uk
Sun Apr 19 11:00:10 EDT 2020
Hi Alan,
Have tried amending to the below:
def update(self):
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") + f"{total_price}"
self.update_idletasks()
if self.state == 'working':
self.lever_btn.after(100, self.update)
But providing me this error:
NameError: name 'symbol' is not defined
self.price_lbl['text'] = f"{self.symbol}" + _("Price = %f") + f"{total_price}"
If I try including self.symbol I get:
AttributeError: 'BeginFuellingPage' object has no attribute 'symbol'
self.price_lbl['text'] = f"{self.controller.symbol}" + _("Price = %f") + f"{total_price}"
If I try including self.controller.symbol, I get:
AttributeError: '_tkinter.tkapp' object has no attribute 'symbol'
Do I need to define it in the BeginFuellingPage, as the only other place symbol is referenced is in SelectCurrencyPage?
class SelectCurrencyPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.currency_lbl = tk.Label(self, text = _("PLEASE SELECT CURRENCY"), font = LARGE_FONT)
self.currency_lbl.pack(pady = 10, padx = 10)
self.currency_buttons = [
self.makeCurrencyButton(*currency)
for currency in CURRENCIES
]
self.restart_btn = tk.Button(self, text = _("RESTART"),
command = lambda: self.controller.setState('restart'))
self.restart_btn.pack()
def makeCurrencyButton(self, code, symbol, conversion):
currency_button = tk.Button(
self,
text=f"{symbol} {code}",
command=lambda: self.setCurrency(conversion)
)
currency_button.pack()
return currency_button
def setCurrency(self, conversion):
self.controller.setCurrency(conversion)
self.controller.setState("fuel")
class BeginFuellingPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.state='idle'
self.fuelcount = 0
self.pricecount = 0
self.begin_fuel_lbl = tk.Label(self, text = _("BEGIN FUELLING"), font = LARGE_FONT)
self.begin_fuel_lbl.pack(pady = 10, padx = 10)
self.litre_lbl = tk.Label(self, text = _("Fuel = 0"))
self.litre_lbl.pack(pady = 10, padx = 10)
self.price_lbl = tk.Label(self, text = _("Price = 0"))
self.price_lbl.pack(pady = 10, padx = 10)
self.lever_btn = tk.Button(self, text = _("FUEL LEVER"),
command = self.fuel_lever)
self.lever_btn.pack()
self.lever_btn.bind('<Button-1>', self.press)
self.lever_btn.bind("<ButtonRelease-1>", self.stop)
self.finish_btn = tk.Button(self, text = _("FINISH"),
command = self.doFinish)
self.finish_btn.pack()
self.restart_btn = tk.Button(self, text = _("RESTART"),
command = lambda: self.controller.setState('restart'))
self.restart_btn.pack()
def fuel_lever(self):
self.begin_fuel_lbl.config(text = _("FUELLING IN PROGRESS"))
self.restart_btn.destroy()
def press(self,evt):
self.state='working'
self.lever_btn.after(100,self.update)
def update(self):
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") + f"{total_price}"
self.update_idletasks()
if self.state == 'working':
self.lever_btn.after(100, self.update)
def stop(self,evt):
self.state = 'idle'
def doFinish(self):
self.begin_fuel_lbl.config(text = _("FUELLING COMPLETE"))
self.lever_btn.config(state = 'active')
self.finish_btn.config(state = 'disabled')
self.controller.setState('finish')
Kind regards,
Jon
________________________________
From: Tutor <tutor-bounces+jon_davies17=hotmail.co.uk at python.org> on behalf of Alan Gauld via Tutor <tutor at python.org>
Sent: 19 April 2020 15:21
To: tutor at python.org <tutor at python.org>
Subject: Re: [Tutor] Fwd: Re: Dynamic variables changed via Tkinter button
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
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list