Matlab GUI: Selecting a number from a popup menu - string

I have a variable d of size 50 x 1 and class double which contains numbers. I am trying to include this in a popup menu in a Matlab GUI. I am currently doing this:
d = cellfun(#num2str, num2cell(handles.zRaw(:)), 'uniformoutput', false);
S.T2pm5 = uicontrol('Style', 'popupmenu', 'BackgroundColor', 'w', 'Parent',...
T2vbox6, 'String', {'Choose a Number', d{:}}, 'value', 1);
However, any selection from the above popup menu using str2double(get(S.T2pm5, 'value')); outputs all the numbers into a single string variable. How can I output only a single number selected by the user through the popup menu than all the values present there?

You can do that by adding a callback that uses both the value and string properties of the popup menu. Here's a working example:
Create a popup menu with a callback
T2vbox6 = figure();
handles.zRaw = [10,20,30,40,50];
d = cellfun(#num2str, num2cell(handles.zRaw(:)), 'uniformoutput', false);
S.T2pm5 = uicontrol('Style', 'popupmenu', 'BackgroundColor', 'w', 'Parent',...
T2vbox6, 'String', {'Choose a Number', d{:}}, 'value', 1,...
'callback', #someMenuCallBack);
str2double(get(S.T2pm5, 'value'))
Then define the call back:
function someMenuCallBack(hObj,event)
d = str2double(get(hObj, 'String'));
val = get(hObj,'Value');
disp(d(val))
This will display the value selected by the user at the command line.

Related

Select the text of a TextEdit object in egui

Is it possible to select all of the text in a TextEdit widget upon a double-click event? I know how to detect the double-click event, I just can't see any way to select all text.
let text_edit = egui::TextEdit::singleline(&mut parameter.str_value)
.desired_width(50.0);
let output = text_edit.show(ui);
if output.response.double_clicked() {
// What to do here?
}

Google script string with style

I'm quite puzzled with how strings are handled in Google Script. In particular, it seems that strings can be styled, but I have no idea how to actually do this.
For instance: I create a Google Form, add a Short Text question, and copy paste a bold text generated here : https://lingojam.com/BoldTextGenerator
If I open this form with a script, I can recover this text with
var form = FormApp.openById(formID);
var Items = form.getItems();
var test = Items[0].getTitle();
This "test" variable is a string (I checked with Logger.log(typeof(test))), not a "Text" nor "RichText" and methods such as .isBold() won't work.
However, Logger.log(test) does output a bold text in the log journal - so this string does contain some information about its style.
Yet, I can't seem to define a styled string within Google Script. I have tried quite different things, but none worked
var dummy = "Hello World !"
Logger.log(dummy.bold())
Logger.log(dummy.setBold(true))
Logger.log(dummy.setFontWeight("bold"))
Logger.log("<b>"+dummy+"</b>")
Logger.log("**"+dummy+"**")
What can I do to have my dummy string to be logged in a bold font (my real objective being to use a .setTitle(dummy) method to have a bold font form item) ?
I believe your goal as follows.
You want to set the text with the bold type to the title of items on Google Form using Google Apps Script.
Issue and workaround:
Unfortunately, in the current stage, there are not methods for directly managing the rich texts to the title of each items on Google Form in Google Form service. But when the text with the bold type is directly copied and pasted to the title of item on Google Form, it can be done. So in this answer, using this, as a current workaround, I would like to propose to convert the text data to the text with the bold type with the unicode, and put the converted text to Google Form.
The flow of this workaround is as follows.
Convert the text to the bold type with the unicode.
In this conversion, each character in the text is converted to the bold type using the difference between the original character code and the bold character code.
Put to the converted text to the title of item on Google Form.
When above flow is reflected to the script, it becomes as follows.
Sample script:
function myFunction() {
// 1. Convert the text to the bold type with the unicode.
const conv = {
c: function(text, obj) {return text.replace(new RegExp(`[${obj.reduce((s, {r}) => s += r, "")}]`, "g"), e => {
const t = e.codePointAt(0);
if ((t >= 48 && t <= 57) || (t >= 65 && t <= 90) || (t >= 97 && t <= 122)) {
return obj.reduce((s, {r, d}) => {
if (new RegExp(`[${r}]`).test(e)) s = String.fromCodePoint(e.codePointAt(0) + d);
return s;
}, "")
}
return e;
})},
bold: function(text) {return this.c(text, [{r: "0-9", d: 120734}, {r: "A-Z", d: 120211}, {r: "a-z", d: 120205}])},
italic: function(text) {return this.c(text, [{r: "A-Z", d: 120263}, {r: "a-z", d: 120257}])},
boldItalic: function(text) {return this.c(text, [{r: "A-Z", d: 120315}, {r: "a-z", d: 120309}])},
};
var newTitle = "New title for item 1";
var convertedNewTitle = conv.bold(newTitle); // Bold type
// var convertedNewTitle = conv.italic(newTitle); // Italic type
// var convertedNewTitle = conv.boldItalic(newTitle); // Bold-italic type
// 2. Put to the converted text to the title of item on Google Form.
var formID = "###"; // Please set the Form ID.
var form = FormApp.openById(formID);
var Items = form.getItems();
Items[0].setTitle(convertedNewTitle);
}
In this sample script, the bold, italic and bold-italic types can be converted.
In this case, the numbers and the specific characters have no bold, italic and bold-italic types. Please be careful this.
Result:
When above sample script is used, the following result is obtained.
From:
To:
Testing
https://jsfiddle.net/7bL5r3em/
References:
Unicode font
https://lingojam.com/BoldTextGenerator
This is from your question.

PySimpleGui: How to enter text in the text box?

I am learning PySimpleGui by referring the tutorials at
Link-1 and Link-2
I need to add buttons to my layout to enter a value, then display the value in adjoining textbox
So far, i have been able to create the buttons and the textboxes.
Following is my code:-
import PySimpleGUI as sg
layout = [[sg.Text('Enter Value:')],
[sg.Input(do_not_clear=False), sg.Text('Value selected is:'), sg.Text(sg.InputText(""), key='_USERNAME_')],
[sg.Button('Enter'), sg.Exit()],
[sg.Text('List Of Values:')],
[sg.Listbox(values=('value1', 'value2', 'value3'), size=(30, 2), key='_LISTBOX_')]]
window = sg.Window('My Application', layout)
while True:
event, values = window.Read()
print(event, values)
if event is None or event == 'Exit':
break
if event == 'Enter':
window.Element('_LISTBOX_').Update(values=[event, values, 'new value 3'])
#window.Element('_USERNAME_').Update(values=[values]) #need to update the text box with value entered
window.Close()
However, i am not able to display the entered value in the text box.
I have added a comment in the code (which gives an error for now) where i need to update the text box with entered value.
Please help!
Edit: I was able to display the value in a popup, but i need to display in the text box
You can update elements directly by referencing them using their key on the window object:
eg as per your updates
window['_LISTBOX_'].Update(values=[event, values, 'new value 3'])
window['_USERNAME_'].Update(values[0])
I figured it out,
Following code serves my purpose:-
import PySimpleGUI as sg
layout = [[sg.Text('Enter Value:')],
[sg.Input(do_not_clear=False), sg.T('Not Selected ', size=(52,1), justification='left',text_color='red', background_color='white', key='_USERNAME_')],
[sg.Button('Enter'), sg.Exit()],
[sg.Text('List Of Values:')],
[sg.Listbox(values=('value1', 'value2', 'value3'), size=(30, 2), key='_LISTBOX_')]]
window = sg.Window('My Application', layout)
while True:
event, values = window.Read()
print(event, values)
if event is None or event == 'Exit':
break
if event == 'Enter':
window.Element('_LISTBOX_').Update(values=[event, values, 'new value 3'])
window.FindElement('_USERNAME_').Update(values[0])
window.Close()

How can I get the values of my widget items to display in a TextArea and convert the values to dictionary

I am creating a post form that will send my input as dictionary to a server. I planned to display the user entered input in a TextArea with my first display button click. This display button sets the next button (Send) to be visible. The Send button then convert or saves the values to a dictionary (key/value pair).
In my code, i followed the example in the ipywidget documentation (https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Styling.html [cell 12]) by adding all my widgets in a list. I am have difficulty in calling the values of the widget items to display in the Text area
I have tried to access the values using children[0].values but get error each time. AttributeError: 'Button' object has no attribute 'values'. I will appreciate anyone's help to do this form. If i can get my inputs as a dictionary, that will be very helpful
from ipywidgets import Layout, Button, Box, FloatText, Textarea, Dropdown, Label, IntSlider, Text
#Form item layout
form_item_layout = Layout(
display='flex',
flex_flow='row',
justify_content='space-between'
)
#form container
form_items = [
Box([
Label(value='Name'),
Text(value='John')
], layout=form_item_layout),
Box([
Label(value='Collection UUID'),
Dropdown(options=['theme', 'play', 'character'])
], layout=form_item_layout),
Box([Label(value='Result'),
Textarea(rows = 20)], layout=form_item_layout),
Button(description="display"),
Button(description="Send", button_style='success')
]
#box layout that holds the forms
box_lyt = Layout(
display='flex',
flex_flow='column',
border='solid 2px',
align_items='stretch',
width='50%'
)
form = Box(children=form_items, layout=box_lyt)
display(form)
form.children[4].layout.visibility = 'hidden'
def show(b):
form.children[4].layout.visibility = 'visible'
form.children[2] = c.children[0] + c.children[1] + c.children[2]
#Convert textarea values to dictionary
def list_children(c):
return c.children[2].to_dict()
form.children[3].on_click(show)
form.children[4].on_click(list_children )
I expect a widget displaying: Name, ID, result and display button. Clicking the display should show the values in the result TextArea and make the button (send) visible. Clicking the Send button should accept these values from result and save as dictionary. The widget displays but is not responsiveenter image description here

How to convert the normal text to bold text?

I have this code in my Dialog:
//other code
dialog.addText(strFmt("Delete this field's value: %1?", MyTable.FieldTable));
//other code
I have an output looklike:
I know the strUpr function:
dialog.addText(strFmt("Delete this field's value: %1?", strUpr(MyTable.FieldTable)));
Does there exist a method or function to convert only the FIELDValue to bold text?
You can set the bold property to 7 on FormBuildStaticTextControl.
Control can be obtained via the control method on DialogText returned by addText method.
The integer that is returned contains the weight of the font as follows:
0 Use the default font weight.
1 Thin.
2 Extra-light.
3 Light.
4 Normal.
5 Medium.
6 Semibold.
7 Bold.
8 Extra-bold.
9 Heavy.
Example:
Dialog dialog = new Dialog();
DialogText dt = dialog.addText("Test");
FormBuildStaticTextControl txtCtl = dt.control();
txtCtl.bold(7);
dialog.run();
A working example using addFieldValue (similar to Matej's solution):
Dialog dialog = new Dialog("Dialog example");
DialogField f1 = dialog.addFieldValue(extendedTypeStr(String30), 'Value', "Delete this field's value?");
FormBuildStringControl c1 = f1.control();
c1.allowEdit(false);
c1.skip(true);
c1.bold(7);
c1.viewEditMode(ViewEditMode::View);
dialog.run();
I doubt that Dialog class has the ability to display bold text, at least I haven't seen such, neither did I find any method to format text in a dialog. One solution would be to create custom form that looks like a dialog box, but I think it is redundant.
Regarding uppercase, you can use strUpr (link) method:
//other code
dialog.addText(strUpr(strFmt("Delete this field's value: %1?", MyTable.FieldTable)));
//other code

Resources