ipywidget Combobox widget got triggered as typing - combobox

I am using Combobox widget and have that widget observe on a give function call. However the issue I ran into, is as I/user type the "input" in the combobox, the function got called multiple times.
Ideally I would only want the Observe function be called after I complete the input and press "enter".
Here is an example:
example_widget = widgets.Combobox( options = ['A','A1','A10','A101'],
ensure_option = True, description ='Example')
and here is the observe; call back
example_widget.observe(foo, 'value')
def foo(,_):
print('call back got triggered')
so as one type "A10" progressively, the call back was triggered three times, namely as A, A1, A10 were typed.
The ideal behaviour is that I input A10, and then press "enter", the call back function foo() only got executed once.
Thanks

example_widget = widgets.Combobox( options = ['A','A1','A10','A101'],
ensure_option = True, description ='Example', continuous_update = False)
need to set continuous_update to False.

Related

RPS, windows Form & Revit API (Python)

I've been trying to build a form to create and delete Revit print Sets.
I've 2 main issues:
1) I'm able to create a print set but I cannot access its content unless I restart the Form. I get the errors below (depending if I'm defining the view_set variable or not)
List_object_has_no_attribute_Views
Local_variable_referenced_before_assignment
This is the code of the function to display the sheets of the selected Print Set
def DisplaySheetsInSet (self, sender, args):
self.curItem = CurrentSetsListBox.SelectedItem
PrintSetForm_Load
try:
view_set=[]
for i in PrintSetForm.ViewSets:
if i.Name == str(self.curItem):
view_set = i
else:
continue
Sheets=[sheet.Name for sheet in view_set.Views]
SheetsLb.BeginUpdate()
SheetsLb.Items.Clear()
for sheet in Sheets:
SheetsLb.Items.Add(sheet)
SheetsLb.EndUpdate()
except Exception as e:
popup (str(e)
2) I'm able to delete print sets once. If I try do delete another one I get the following error and I need to restart the form ( code for the function that deletes the print sets shown below)
The_referenced_object_is_not_valid
def DelPrintSet(self, sender, args):
self.curItem = CurrentSetsListBox.SelectedItems
t = Transaction (doc, 'Delete printset')
t.Start()
for viewset in PrintSetForm.ViewSets:
if viewset.Name in [str(item) for item in self.curItem]:
doc.Delete(viewset.Id)
doc.Regenerate()
else:
continue
self.Refresh()
UpdateSetNames(CurrentSetsListBox)
t.Commit()
I've tried to build a function to restart/refresh the Form but it doesn't work (code below):
global PrintSetForm_Load
def PrintSetForm_Load(self, sender):
Application.Exit()
Application.Restart()
#self.Refresh()
#self.ResetBindings()
#self.ActiveForm.Close()
sd = PrintSetForm()
sd.ShowDialog()
This gif shows the form in action:
Manage Print Sets
Any ideas or suggestions?
Thank you.
3) If I try to populate the SheetsLb with a DataSource, just the first set clicked is shown.
Sheets=[sheet.Name for sheet in view_set.Views]
SheetNumber=[sheet.get_Parameter(BuiltInParameter.SHEET_NUMBER).AsString() for sheet in view_set.Views]
SheetsLb.BeginUpdate()
SheetsLb.DataSource = None
SheetsLb.Items.Clear()
UpdatedList=[]
for number,name in zip(SheetNumber,Sheets):
UpdatedList.append(number+" - "+ name + " [ ] ")
SheetsLb.DataSource=UpdatedList
SheetsLb.EndUpdate()
1) See if this works:
It would be worth checking that there is something selected in self.viewSetsLb. Ive added a check to the code below
The view_set variable could be initialised as a boolean instead of a list
Using break in the for loop keeps things a little snappier
Ive used the more pythonic for view in PrintSetForm.viewSets rather than for i in PrintSetForm.viewSets - keeping it nice and clear
This code works for me:
self.curItem = self.viewSetsLb.SelectedItem
if not self.viewSetsLb.SelectedItem:
print 'No Printset selected!'
return
view_set = False
for view in PrintSetForm.viewSets:
if view.Name == str(self.curItem):
view_set = view
break
else:
continue
Sheets=[sheet.Name for sheet in view_set.Views]
self.sheetsLb.BeginUpdate()
self.sheetsLb.Items.Clear()
for sheet in Sheets:
self.sheetsLb.Items.Add(sheet)
self.sheetsLb.EndUpdate()
2) Its because the data in your PrintSetForm.ViewSets list is out of date. Every time you change something (ie delete a viewset), repopulate this list:
PrintSetForm.ViewSets = FilteredElementCollector(doc).OfClass(ViewSheetSet).ToElements()
Also, you shouldnt need to build a refresh button, perhaps have a class function that repopulates the Printset list and ListBox, and clears the Sheet ListBox that you call after every action?
Sounds like youre having fun mate!
It sounds as if you have an issue with the scoping and lifetime of variables. For instance, some variables may have a lifetime limited to the form display, and therefore cannot be accessed after the form is closed. You could change the lifetime of these variables, e.g., by making them static class variables instead of local instance variables. I suggest you read up on .net static class variable scope.

How to make a button API

I am trying to make an easy to use button API in Lua with ComputerCraft, and I'm having some trouble. When I do:
os.loadAPI("button")
action=function()
term.clear()
term.setCursorPos(1,1)
print("Hello!")
end
button.newButton("B1",5,5,20,10)
button.drawButton("B1",colors.orange,colors.white)
button.onClick("B1",action,true)
Nothing happens, it doesn't even draw the colors. I have done tests, and when I store something like colors.white as a variable, then print the variable, it returns the number code of that color, which comes from the colors API. Here is what I have:
--to use the newButton function, do this:
--button.newButton(exampleButton)
--to use onClick function, create a variable like this:
--exampleFunc=function()
--(code)
--end
--Then call onClick with the same variable:
--button.onClick(exampleButton,exampleFunc)
buttons={}
xPos=0
yPos=0
function removeButton(buttonName)
for key, fields in pairs(buttons) do
if key == buttonName then
table.remove(button,buttonName)
else
print("ERROR: button name not available")
end
end
end
function onClick(buttonName,action,boolean)
for key, fields in pairs(buttons) do
if boolean then
testClick(action)
end
end
end
function drawSeparateButton(x,y,w,h,outLineColor,fillColor)
if key == buttonName then
x=buttons[buttonName]["x"]
y=buttons[buttonName]["y"]
w=buttons[buttonName]["w"]
h=buttons[buttonName]["h"]
paintutils.drawBox(x,y,x+(w-1),y+(h-1),outLineColor)
paintutils.drawFilledBox(x+1,y+1,x+(w-2),y+(h-2),fillColor)
end
end
function testClick(action)
for key, fields in ipairs(buttons) do
x=buttons[buttonName]["x"]
y=buttons[buttonName]["y"]
w=buttons[buttonName]["w"]
h=buttons[buttonName]["h"]
x2=x+(w-1)
y2=y+(h-1)
button,xPos,yPos=os.pullEvent("mouse_click")
if xPos>=x and xPos<=x2 and yPos>=y and yPos<=y2 then
action()
end
end
end
function newButton(buttonName,X,Y,W,H)
buttons[buttonName] = {x=X,y=Y,w=W,h=H}
end
function drawButton(buttonName,outLineColor,fillColor)
for key, fields in ipairs(buttons) do
if key == buttonName then
x=buttons[buttonName]["x"]
y=buttons[buttonName]["y"]
w=buttons[buttonName]["w"]
h=buttons[buttonName]["h"]
x2=x+w-1
y2=y+h-1
x3=x+1
y3=y+1
x4=x+w-2
y4=y+h-2
paintutils.drawBox(x,y,x2,y2,outLineColor)
paintutils.drawFilledBox(x3,y3,x4,y4,fillColor)
elseif key ~= buttonName then
print("Button name not availabel")
end
end
end
I just need to be able to store a color like colors.white in a variable and have it returned as colors.white, and not the color code. I also need to be able to check which button is clicked and run a function specified by the user when one of the buttons are clicked.
I'm going to walk through your prototype code and point out some errors I see and also try to answer your question. I'm going to assume that you want to set a key value in a table to an array and access that externally.
A quick and short answer to your question is that you can store tables within tables and access them through keys or indices. A design change I would make, however, is to store your exampleFunc as a member of each button table to associate it with a specific button.
Example:
buttons = {}
buttons.playButton = {x=0, y=0, w=10, h=10, func=function() return end}
buttons.quitButton = {x=0, y=30, w=10, h=10, func=function() return end}
...
buttons.quitButton.x = 10
buttons.playButton.func()
Tables have a key-value structure, where keys can be strings or numbers. There are multiple ways to access an array using a key depending on the data type of the key.
For example, instead of writing buttons.quitButton.x = 10 we could've written buttons["quitButton"].x = 10 or buttons["quitButton"]["x"] = 10 or buttons.quitButton["x"] = 10.
This page is a great starting point for learning about Lua's tables.
According to this page, os.pullEvent() is blocking, and you will only be able to check if one button is clicked per mouse click. Consider looping through your buttons table and checking every button to see if the mouse falls within its rectangular bounds. Once you find which button the mouse clicked, you can call its func member. While we're still discussing this method, the while true do loop is completely unnecessary.
function removeButton(buttonName)
for buttonName in pairs(button) do
...
function newButton(buttonName)
state=true
for buttonName in pairs(buttons) do
...
You may come from a Python background where the statement if element in list exists but Lua has no such statement. The for loops you use are looping through every member of the list. You also aren't capturing all of the variables returned by the pairs() function. A fix for that would look something like the following:
function buttonFunction(buttonName)
for key, fields in pairs(buttons) do
if key == buttonName then
...
end
end
There are multiple instances where you refer to the variable button when you mean buttons.

Intern test returns incorrect value for range input

I have a test that checks the value an HTML5 range input.
return this.remote
// etc.
.findById('range')
.getAttribute("value")
.then(function(val){
expect(parseInt(val)).to.equal(2);
});
The value is correct when I check its initial value, but if I change the value then check, it has not been updated. I found that the value doesn't update in the developer tools either. I tried using
.sleep(3000)
between changing the value and calling
.getAttribute('value')
but that didnt' seem to be the issue.
In this JSfiddle, inspecting the range element with your browser's developer tools will show the title change, but the value does not (even though the value is correctly changed in the textbox). So this may be an issue with the webdriver, but I'd like to know if anyone has run into this issue.
Is this related to the test's failure to get the updated value? Is there another method I can use to read values(attributes)?
Edit:
It seems like the browser's onchange/oninput event is not triggering properly (similar problems: WebDriver: Change event not firing and Why does the jquery change event not trigger when I set the value of a select using val()?), and the webdriver is possibly not able to, either. Do I have to add Jquery as a define for my test, even though I only need to use trigger() ? Or is there another solution?
Edit2: I've added a better example of how I'm using the range input in this new JSfiddle. I added plus/minus buttons, which fail to trigger the change event that should update the value attribute of the range input, (and which fails to enter the value into the textbox).
You could fire the change event manually in your test. I was able to get the 'textValue' input in your JSFiddle to update that way and I imagine it might work similarly in your test.
rangeBar = document.querySelector('#range');
function myFire(element, eventType) {
var myEvent = document.createEvent('UIEvent');
myEvent.initEvent(
eventType, // event type
true, // can bubble?
true // cancelable?
);
element.dispatchEvent(myEvent);
}
myFire(rangeBar, 'change');
This comes up often enough that I have a helper in my base test class (Java)
public enum SeleniumEvent
{blur,change,mousedown,mouseup,click,reset,select,submit,abort,error,load,mouseout,mouseover,unload,keyup,focus}
public void fireEvent(WebElement el, SeleniumEvent event)
{
((JavascriptExecutor) getDriver()).executeScript(""+
"var element = arguments[0];" +
"var eventType = arguments[1];" +
"var myEvent = document.createEvent('UIEvent');\n" +
"myEvent.initEvent(\n" +
" eventType, // event type\n" +
" true, // can bubble?\n" +
" true // cancelable?\n" +
");\n" +
"element.dispatchEvent(myEvent);", el, event.toString());
}
Another thought. getAttribute("value") might not be getting what you think it does. In JavaScript, document.querySelector('#range').getAttribute('value') always returns the hard-coded value attribute (i.e. the default or initial value), not the input's current value.
document.querySelector('#range').value returns the current value.

wxpython SetFocus and EVT_KILL_FOCUS combobox

I am trying to understand how EVT_KILL_FOCUS and SetFocus on a combobox works .Here is the following code:
import wx
class MainWindow(wx.Frame):
count = 1
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(150,150), style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER ^ wx.MAXIMIZE_BOX)
panel = wx.Panel(self, -1)
self.Calc_Display = wx.ComboBox(panel, -1, style=wx.CB_DROPDOWN|wx.TE_PROCESS_ENTER)
self.Calc_Display.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
btn = wx.Button(panel, label="Test")
MainFrameSizer = wx.BoxSizer(wx.VERTICAL)
MainFrameSizer.Add(self.Calc_Display, 0, wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 5)
MainFrameSizer.Add(btn, 0, wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 5)
panel.SetSizer(MainFrameSizer)
panel.Fit()
self.Show()
def OnKillFocus(self, event):
print ('EVT_KILL_FOCUS was called %i times' %MainWindow.count)
MainWindow.count += 1
# self.Calc_Display.SetFocus()
event.Skip()
app = wx.App(False)
frame = MainWindow(None, "Test")
app.MainLoop()
If run this, you will get a print every time the combobox loses focus, which is expected.
If you uncomment line 28, that's when it gets weird. The EVT_KILL_FOCUS gets called over and over. That is the behavior I am trying to understand. In the program I am writing, I want to force the combobox to maintain focus in case a condition is not satisfied. Are there any ways around this behavior?
I found this link on the issue, but I did not quite understand it: http://wiki.wxpython.org/Surviving%20with%20wxEVT%20KILL%20FOCUS%20under%20Microsoft%20Windows
Thanks for any help
After reading the link posted in your question, and playing around with your code, I came to the conclusion that calling SetFocus() on the combobox is apparently triggering the EVT_KILL_FOCUS in Windows. I had noticed that on running the program, it prints the statement once, although It shouldn't since the combobox is not losing focus.
I tried two things:
First was to move the combo box code after the button code. In this case when the program is run the Focus is on the button, and sure enough the statement is not printed!
Here's the edit:
btn = wx.Button(panel, label="Test")
self.Calc_Display = wx.ComboBox(panel, -1, style=wx.CB_DROPDOWN|wx.TE_PROCESS_ENTER)
self.Calc_Display.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
Second was to try binding the btn to a handler which sets the focus on the combobox
btn.Bind(wx.EVT_BUTTON, self.On_Button)
def On_Button(self, event):
self.Calc_Display.SetFocus()
In this case the statement was being printed twice every time the button was being clicked. I guess this is once for when the focus is lost and once for setting back the focus. Note that the SetFocus() is still commented out in the OnKillFocus in this case.
Back to your code: If the SetFocus is "uncomment-ed" the statement is printed many times because it keeps setting the focus which in turn triggers the event and it goes on in a loop. This may be a wrong explanation, but nevertheless it is the only thing I could think of.

Why is this method executing twice each time I call it?

I have the following method that is executing twice every time it is called:
public static void ChangeToRepository(RepositoryTextBox textBox, int repositoryNumber)
{
MessageBox.Show("you");
int indexOfLastRepository = (textBox.RepositoryCollection.Count - 1);
if (repositoryNumber > indexOfLastRepository)
{
AddTextRepositoriesThrough(textBox, repositoryNumber, indexOfLastRepository);
}
textBox.RepositoryCollection[textBox.CurrentRepositoryNumber].CurrentText = textBox.Text;
textBox.PreviousRepositoryNumber = textBox.CurrentRepositoryNumber;
textBox.CurrentRepositoryNumber = repositoryNumber;
textBox.Text = textBox.RepositoryCollection[textBox.CurrentRepositoryNumber].CurrentText;
}
The first time that the method executes, it executes all of the code except for its last line:
textBox.Text = textBox.RepositoryCollection[textBox.CurrentRepositoryNumber].CurrentText;
The second time, it executes all of the code. What's up?
When you assign to CurrentRepositoryNumber on the text box, it probably triggers an event handler that calls back to this function again. This seems likely because the property name suggests that it controls the current repository, which this method then is responsible for displaying somehow.
You might want to temporary delist, assign to the property and then re-enlist that event handler. Or maybe you need more of a redesign to get the responsibilities clear - often with GUI frameworks that is hard to do, and the simplest option is to just delist, assign, re-enlist, with this kind of pattern:
textBox.TextChange -= YourHandler;
textBox.Text = newValue;
textBox.TextChange += YourHandler;

Resources