The typical way to create a button in python with widgets is as follows:
button = wd.Button(description = "Click")
output = wd.Output()
display(button, output)
def on_button_clicked(b):
with output:
print("button clicked")
button.on_click(on_button_clicked)
when clicking the button the corresponding method runs.
Is it possible to distinguish between simply click and CTRL+click?
i.e. having like two methods,on_click, or on_CTRL_click
thanks
Related
I am trying to get the value of a combobox on a button click and process the value in the button click handler. However, the combobox.v_model seems to be updated correctly only after the button click handler has exited.
Here is what I did (code below):
enter string 'xxx' in the combobox when widgets show up
click on the button thereafter and fetch the value of combobox.v_model
Expected to retrieve 'xxx', but retrieved '' (empty string) instead
Is there a way to retrieve the combobox content with a button click immediately after input?
Note: when 'Enter' / 'TAB' is pressed before the button click, all works, but not if the button is pressed immediately after input in the combobox.
import ipyvuetify as vue
name = ''
# vuetify combobox and button
combobox = vue.Combobox(label="Enter name", v_model="", items=[], autofocus=True)
btn = vue.Btn(children=['Process name'])
component = vue.Row(children=[
vue.Col(children=[combobox]),
vue.Col(children=[btn])
])
# --- event handler -------------------------
# Some processing of the combobox input needs to happen
# on a button click, but v_model is not updated
def on_button_clicked(widget, event, data):
print(f"btn clicked: {combobox.v_model=}")
name = combobox.v_model
print(f'btn clicked: {name=}')
# do some processing with name here
btn.on_event("click", on_button_clicked)
display(component)
Could be a bug in Vuetify: vuetifyjs/vuetify#12567 (also refer to this post on the ipyvuetify issue tracker).
I am using QFileDialog module, and I encounter a problem: how do I know which button is clicked (save or cancel) in the popup window when using QFileDialog.getSaveFileName in PyQt5:
The QFileDialog::getSaveFileName() method returns an empty string if the file was not chosen, that is, when the cancel button is pressed, and instead a non-empty string when the save button is pressed:
filename, _ = QFileDialog.getSaveFileName()
if filename:
print("The save button is pressed")
else:
print("The cancel button is pressed")
I am trying to initialize a variable with the text contained in a textbox when the button is pressed , in a jupyter notebook running python 3. However it seems that I can't access the value in the textbox during the button pressing event and store it for later.
Here is the code :
import ipywidgets as widgets
from IPython.display import display
button = widgets.Button(description="Click Me!")
inp = widgets.Text(description='Path:')
Box = widgets.HBox([button,inp])
def on_button_clicked(b):
return inp.value
path = button.on_click(on_button_clicked)
display(Box
)
Any help would be greatly appreciated.Thanks in advance
The button.on_click function returns None. It doesn't return the value of the function you pass in as an argument. Think of on_click as connecting the button to a function that has side effects. In the example below I use it to append to a list.
import ipywidgets as widgets
from IPython.display import display
button = widgets.Button(description="Click Me!")
inp = widgets.Text(description='text:')
Box = widgets.HBox([button,inp])
value_list = []
def on_button_clicked(b):
value_list.append(inp.value)
print(value_list)
button.on_click(on_button_clicked)
Box
Your value will be available through
inp.value
Please see the documentation
Trying to create a simple button with some output I tried the following code
from IPython.display import display
def clicked():
print("button has been clicked!")
button_download = widgets.Button(description = 'Test Button')
button_download.on_click(clicked)
display(button_download)
but when I click on the button I cannot see any output.
I found another example that works, but this is way too complicated:
from IPython.display import display
button = widgets.Button(description="Click Me!")
output = widgets.Output()
display(button, output)
def on_button_clicked(b):
with output:
print("Button clicked.")
button.on_click(on_button_clicked)
Do I really need to output thing so I can see the output of a print statement when I click the button?
The system is Jupyterlab 1.1.4.
You only need to add an argument to the clicked function to make it work:
from IPython.display import display
import ipywidgets as widgets
def clicked(arg):
print("button has been clicked!")
button_download = widgets.Button(description = 'Test Button')
button_download.on_click(clicked)
display(button_download)
The radio buttons are in a submenu off the main menubar.
Cannot get the value of the radio box clicked in PySide this worked perfectly in Tkinter
and also how would one set default radio button checked in PySide?
in PySide: Cannot get value of radio box when changed/selected in pyside. This Works perfectly in tkinter below
os.chdir(user+'database')
dbfiles=glob.glob('ias*.db')
#self.var=('ias'+yr+'.db') used to set default radio button? somehow
self.ag = QActionGroup(self)
for self.x in dbfiles: #dbfiles is a "list" of 3 databases
dbselect = self.ag.addAction(QAction(self.x, self, checkable=True))#how to set default?
self.menuDatabases.addAction(dbselect)
self.ag.triggered.connect(self.displaydb) #need to trigger something else?
def displaydb(self):
print(self.x) #need to print something else maybe?
In TK; Works perfectly here change radio button and will print database name to textbox
os.chdir(os.path.expanduser('~')+'/Desktop/data/database')
self.dbfiles=glob.glob('ias*.db')
self.var=StringVar()
self.var.set('ias'+str(dte)+'.db')
for x in self.dbfiles:
dbfle.add_radiobutton(label=x,variable=self.var, value=x,command=self.sel)
def sel(self):
selection = "You changed to database: " + str(self.var.get())
self.b.delete(1.0,END)
self.b.insert(END,selection)
To set one of the actions checked at the start:
self.ag.actions()[0].setChecked(true)
#selects first action, or do this at creation time
and change the callback:
def sel(self, action):
print action.text()