2

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

Agarest
  • 29
  • 5

4 Answers4

2

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)
chunpoon
  • 950
  • 10
  • 17
0

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')
Nikolay D
  • 11
  • 3
0

In a more pythonic way:

parm = 123
window['Text1'].update('Voltage: {} V'.format(str(parm)))
felixpradoh
  • 135
  • 7
0

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}")
Mike from PSG
  • 5,312
  • 21
  • 39
  • Note that this is an answer in July 2022.... who knows how long it will be an "optimal" answer. StackOverflow suffers from the "nothing ever dies here" problem and the highest-rated answer may in fact be the worst solution years down the road. – Mike from PSG Jul 19 '22 at 12:57