PySide - How to dynamically update a string value based on QLineEdit Text value? - pyqt4

How do I make my string dynamically updated every time the Text value from a QLineEdit changes? I tried using signals and slots and works fine with QLabel, but not string.
self.lnEditCurrentNS = QtGui.QLineEdit(self) # This is my QLineEdit
my_string = 'none'
self.lnEditCurrentNS.textChanged.connect(self.onSelectedValue)
def onSelectedValue(self):
selectedValue = self.txtEditCurrentNS.displayText()
my_string = selectedValue
Let's say currently lnEditCurrentNS = 'none', then its changed to 'hello'. The value of my_string remains as 'none' instead of getting updated to 'hello'.
The value of lnEditCurrentNS got updated dynamically when the current index of a combobox I have is changed (combobox = 'hello', lnEditCurrentNS = 'hello')
I'm not very familiar with PySide/PyQt so any guidance is highly appreciated. Thanks.

You declared my_string outside the onSelectedValue, you assigned a value inside onSelectedValue. Now if you use my_string outside the onSelectedValue, it won't give the lineEdit's text.
You can do the following trick:
self.my_string = 'None'
...
def onSelectedValue(self):
self.my_string = self.textEditCurrentNS.text()
You can now use self.my_string at any place in the code to have the current text of the lineEdit.

Related

StringVar() with Listbox multiple or extended - lists converting to string

I have a tkinter Listbox and I'm using a StringVar().set() to set the values from a list. When I get the values back using StringVar().get(), they are converted to an awkward string format, like this:
"('Item1', 'Item2', 'Item3')"
What's the best way to avoid this conversion in the first place, or failing that, to convert the string back to the initial list?
You can use this snippet of code to reproduce the problem in its most simple form:
import tkinter as tk
root = tk.Tk()
values = tk.StringVar(root)
values.set(['Item1', 'Item2','Item3'])
print(values.get())
It's not pretty, but I had come up with this:
values.get()[2:-2].split("', '")
As StringVar is inherited from Variable with overridden get() function which converts the stored value to string.
Using Variable will not have such conversion.

Insert variable number of items with FORMAT ( )

I have a text with some strings that I want to replace with a variable. For example:
message = """I am a message for {user} and you have puchased the following items {items} with color {color}"""
There I want to replace {user}, {items} and {color} by a variable using the following code:
message = message_template.format(user='Ali', ID = ID1)
The problem is that in some cases I will have one item and in other cases more than 5 and I need to insert them independently. Also, color and item are part of a Dataframe.
Any idea about how could I insert a changing number of variables with .format( )?
Thanks
As for multiple items convert your list to a string using : ', '.join(items)
items = ['i1','i2','i3']
message = message_template.format(user='Ali', items = ', '.join(items), color='orange')

How to use a string as a part of a variable in python

I would like to use a string as a part of the variable in a loop like this:
items = ['apple', 'tomato']
for item in items:
'eat_'+item = open("users/me/"+item+"/file.txt").read()
So that as an output I have 2 variables named eat_apple and eat_tomato.
Obviously, the 'eat_'+item is wrong but to just get my idea.
P.S. I saw similar posts but neither of them helped
Instead of assigning the value to the variable that is a string, you can instead create a dictonary and use the variable name(which is of string type) as key and store a corresponding value to it.
Try it:
items = ['apple', 'tomato']
eat_dict = dict()
for item in items:
eat_dict['eat_'+item] = open("users/me/"+item+"/file.txt").read()

Ampscript BuildRowsetFromString() fails on single item

I've been tasked with an ExactTarget task, which uses Ampscript. Trying to learn on the go here. See code snippet below:
%%[
Var #testString, #testOutput
Set #testString = Qwerty
Set #testOutput = BuildRowsetFromString(#testString,"~")
]%%
TestOutput:%%= v(#testOutput) =%%
The code works if the testString contains a ~, but when there is no ~ character in the string, the ouput is blank. Is this correct by design? Do I need to add a conditional to check for the presence of the ~ character?
That's the expected behavior. The BuildRowsetFromString() function alone isn't going to return any value when displayed, you're going to need to use Row() and Field() in order to pull the value out.
Using your example:
%%[
Var #testString, #testOutput
Set #testString = "Qwerty"
Set #testOutput = BuildRowsetFromString(#testString,"~")
]%%
RowCount: %%=RowCount(#testOutput)=%%
TestOutput: %%=v(#testOutput)=%%
The RowCount() function returns a value of 1, essentially saying it knows there's at least one 'row' in there. To display that one value, you'll need to wrap that value with Field() and Row():
TestOutput: %%=Field(Row(#testOutput,1),1)=%%
If you want to display other values in the string, say you were passing "Qwerty~Second~Third", you'll need to either change the number at the Row() function or perform a loop.
References
Using Loops
BuildRowsetFromString() Function

computed field value in xpages

I am trying to update a computed fields value on click of a button with a value of a edit box + its own value.
Code written on button: here i put value of edit box in scope variable and make edit box blank. comment_te is the name of edit box
requestScope.put("commentValue", getComponent("comments_te").getValue);
getComponent("comments_te").setValue("");
Code written for value of computed field: comments is the name of computed field
getComponent("comments").getValue + "\n" + requestScope.get("commentValue")
But I get the output is:
0 com.ibm.xsp.component.xp.XspInputText#65426542
Please help me with this.
You're missing the parentheses in your calls to getValue(). By omitting these, you're returning a pointer to the getValue method of the component, not the result of invoking that method. Change each reference to getValue to getValue(), and you'll get a different result.
Your code returning the Object.
Try the following.
This following code get the the editbox value and set to a scope variable.
requestScope.commentValue = getComponent("comments_te").value;
getComponent("comments_te").value = "";
This following code sets the value to the computed field.
getComponent("comments").value = getComponent("comments").value + "\n" + requestScope.commentValue;
When you are appending the value to the computed field, as default it will add 0 to its value. Do the validation if you want.
I hope this helps you...!!!

Resources