# Setting widget variable from --python-code on Slicer.exe start

**URL:** https://discourse.slicer.org/t/setting-widget-variable-from-python-code-on-slicer-exe-start/23908
**Category:** Development
**Created:** [June 17, 2022, 2:33am UTC](https://discourse.slicer.org/t/setting-widget-variable-from-python-code-on-slicer-exe-start/23908 "2022-06-17T02:33:59Z")
**Posts on this page:** 1
**Showing post:** 13

<div class="post-metadata">

### Author: ![talmazov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/talmazov/32/3966_2.png) [@talmazov](https://discourse.slicer.org/u/talmazov)
#### Post date: [June 28, 2022, 4:50pm UTC](https://discourse.slicer.org/t/setting-widget-variable-from-python-code-on-slicer-exe-start/23908/13 "2022-06-28T16:50:12Z")

</div>

Hey,  
Apologies it took me a while to respond, thank you so much for your help.  
For future reference to those who stumble on this post I would like to elaborate on the solution.  
@lassoan thank you for the suggestion of improving on the string formatting. Use of

> “\n” and “\t”

within the string was not a problem. Although for future reference it is good to know that I can separate commands inline with semicolon “;”  
The real problem was that I was trying to set

> self.host\_port

by accessing

> slicer.util.getModule(‘BlenderMonitor’).widgetRepresentation().self().[widget init var]

Reviewing @lassoan suggestion, widget class functions are more appropriately accessed by the

> slicer.util.getModuleWidget().[widget class method] ()

method. When I switched to getModuleWidget() and wrapped self.host\_port variable in the self.connect\_to\_blender() method I was able to pass and set self.host\_port on widget startup.

TL;DR @lassoan code suggestions worked well, use of semicolon was not necessary and use of \n and \t in string formatting is acceptable when --python-code

---

_[View the full topic](https://discourse.slicer.org/t/setting-widget-variable-from-python-code-on-slicer-exe-start/23908)._
