Reputation: 29
In Layout:
sg.Txt('', size=(12,1), font=('Helvetica', 15), key='Text1', justification='left', text_color='green'),
Example:
parm = 123
window.Element('Text1').Update("%.2f" % parm)
I would like to understand how to add a fixed text before and after the variable 'parm' that is written in Text1. For example: Voltage: 123V
Thank you
Upvotes: 2
Views: 2570
Reputation: 5754
Using the latest coding conventions it could be perhaps like this, assuming you're running 3.6+ with access to f-strings.
You can remove a number of the parameters from you layout definition now....omitting the size
will enable your Text element to grow and shrink depending on contents. Justification is automatically left so no need for that.
# In your layout:
sg.Text(font=('Helvetica', 15), key='-TEXT1-', text_color='green'),
# later in your event loop
parm = 123
window['-TEXT1-'].update(f"parm has value: {parm}")
Upvotes: 0
Reputation: 135
In a more pythonic way:
parm = 123
window['Text1'].update('Voltage: {} V'.format(str(parm)))
Upvotes: 0
Reputation: 11
generally:
parm = 123
window.Element('Text1').Update(f'text1 {parm} text2')
In your specific question:
parm = 123
window.Element('Text1').Update(f'Voltage: {parm}V')
Another way is:
parm = 123
window.Element('Text1').Update('Voltage: '+ str(parm)+'V')
Upvotes: 0
Reputation: 970
I think the simplest way is to just make a custom function for it.
def custom_update_text(key, parm, symbol="V"):
window[key].Update(f"{str(parm)}{symbol}")
parm = 123
set_voltage('Text1', parm)
Upvotes: 2