IPython nbwidgets: Toggle Visiblity by button click - python

I'm using the ToggleButton and want to link it's value to the visilbiity of another widget.
I've come accross the Widget Events, but it is unclear to me how to bind the style property of the other widget to the value of the ToggleButton.
Has anyone done something similar?

I would use an observe call on the ToggleButton to change the visibility of the other widget. A simple example below.
toggle = ipyw.ToggleButton(description='Toggle visible')
to_hide = ipyw.IntRangeSlider(description = 'hide me')
display(to_hide)
display(toggle)
def hide_slider(widg):
if widg['new']:
to_hide.layout.display = 'none'
else:
to_hide.layout.display = ''
toggle.observe(hide_slider, names=['value'])

Related

How to access a widget in another widget event handler : Tkinter

I am creating a GUI in tkinter having a listbox and a Text item in a child window which appears after a button click. Listbox is displaying values of a dict which are basically names of files/directories in disk image.
I want to change the text of Text widget on <ListboxSelect> event and display type or path of selected file.
Now I cant make Text global since it has to appear on child window, so I need a way to access it in event handler of Listbox. Can I give handler reference of Textbox?
Here is my code;
def command(event):
... #Need to change the Text here, how to access it?
def display_info(dict,filename):
child_w = Tk()
listbox = Listbox(child_w)
textview = Text(child_w)
...
listbox.bind(<ListboxSelect>,command)
def upload_file():
window = Tk()
upl_button = Button(command=upload_file)
window.mainloop()
Is there a way to create a textview as global and then change its properties later to be displayed in child_window etc.
Two solutions here that I can think of is to make textview a globalized variable or to pass textview to command() as an argument.
Parameter solution:
def command(event,txtbox):
txtbox.delete(...)
def display_info(dict,filename):
child_w = Tk()
listbox = Listbox(child_w)
textview = Text(child_w)
...
listbox.bind('<ListboxSelect>',lambda event: command(event,textview))
Or simply just globalize it:
def command(event):
textview.delete(...)
def display_info(dict,filename):
global textview
child_w = Tk()
listbox = Listbox(child_w)
textview = Text(child_w)
...
listbox.bind('<ListboxSelect>',command)
While saying all this, it is good to keep in mind that creating more than one instance of Tk is almost never a good idea. Read: Why are multiple instances of Tk discouraged?

How to choose line-edit according to which button was clicked

I have two buttons (Eg. A andB) which do the same things (based on user selection). So when you select something, then click the button, the selection's name will be input into the line-edit for the button. For example, if I click on buttonA, the input will be to lineEditA.
Currently I have created a signal function as follows:
def _connections_setup(self):
self.btnA.clicked.connect(self.get_sel_nameA)
self.btnB.clicked.connect(self.get_sel_nameB)
def get_sel_nameA(self):
sel_name = get_name_from_sel()
self.line_editA.setText(sel_name)
def get_sel_nameB(self):
sel_name = get_name_from_sel()
self.line_editA.setText(sel_name)
"""
def get_sel_name(self):
# Returns me a blank
button = self.sender()
print button.objectName()
# My objective here would be, if btnA is clicked, the sel_name will be inputted into lineEditA. Likewise for btnB
"""
Instead of creating two similar functions, how can I determine which button was clicked and have the selection's name to be input correctly into the line-edit?
I tried using self.sender() (see get_sel_name()) but it does not seems to return me the button name.
The sender() function only works in slots directly connected to signals. So your code needs to look something like this:
def _connections_setup(self):
self.btnA.clicked.connect(self.get_sel_name)
self.btnB.clicked.connect(self.get_sel_name)
def get_sel_name(self):
button = self.sender()
name = button.objectName()
if button is self.btnA:
self.line_editA.setText(name)
elif button is self.btnB:
self.line_editB.setText(name)

How to find QButton created with a loop?

in maya one creates a button with:
cmds.button('buttonname', label='click me')
where buttonname is the name of
the button object. At a later stage i can edit the button simply by calling:
cmds.button('buttonname', e=1, label='click me again')
Now the problem:
i created a bunch of buttons in qt using a loop:
for s in Collection:
file = os.path.splitext(s)[0]
# Main widget
widgetItem = QtWidgets.QWidget()
layoutItem = QtWidgets.QVBoxLayout()
widgetItem.setLayout(layoutItem)
# Button
button = QtGui.QPushButton()
button.setObjectName(file)
layoutItem.addWidget(button)
How can i call/edit one of them using the button name?
Thanks in advance
Assuming you already have access to their parent widget, you can find them by findChild method.
In C++ syntax, it would be something like this:
QPushButton *button = parentWidget->findChild<QPushButton *>("button1");
where button1 is the name of that button.

QLineEdit not updating text with QKeyEvent

I'm trying to implement a virtual keyboard widget. The simplest way I could think of is to create QKeyEvent instances and send them with QApplication.postEvent() to the widget in focus.
First, I'm trying to update a fixed QLineEdit that I have, so the code is:
self.pushButton_A.clicked.connect(self.virtualKeyPress)
[...]
def virtualKeyPress(self):
self.keyPress = QKeyEvent(QEvent.KeyPress, Qt.Key_A, Qt.NoModifier)
QApplication.postEvent(self.lineEdit, self.keyPress)
But the QLineEdit instance won't update its text in the GUI!
Clues? Cheers and thanks!
RESOLVED: (kudos to HeyYO)
self.pushButton_A.clicked.connect(self.virtualKeyPress)
[...]
def virtualKeyPress(self):
self.keyPress = QKeyEvent(QEvent.KeyPress, Qt.Key_A, Qt.NoModifier, 'A')
QApplication.postEvent(self.lineEdit, self.keyPress)
In my case, inplace of Qt.Key_A I set that argument to 0 so that I can connect all my buttons to the virtualKeyPress method. I also had to set the focus policy for all the buttons to 'no focus' (did it directly in Qt Designer). The final code was the following:
def virtualKeyPress(self):
self.keyPressed = QString(self.sender().text())
self.keyPress = QKeyEvent(QEvent.KeyPress, 0, Qt.NoModifier, self.keyPressed)
self.focusWidget = QApplication.focusWidget()
QApplication.postEvent(self.focusWidget, self.keyPress)
Have you tried specifying the text argument;
self.keyPress = QKeyEvent(QEvent.KeyPress, Qt.Key_A, Qt.NoModifier, "A")
It worked for me, in Qt5&C++, so I'm assuming it will work for you as well.

How to update a Tix.ComboBox's text?

I have a Tix.ComboBox with an editable text field. How do I force the variable holding the value for the text to update?
Let me give a more concrete explanation. I have a combo box and a button. When I click the button, it pops up a message box with the value of the combo box. Let's say the combo box text field currently has the value "thing1". If I type "new" into the box and then click on the button with my mouse, it will pops up the message "thing1". If I type "new" in the box and then tab focus away from the combo box and then click the button the pop up message says "new".
Ho do I force the combo box to update it's value to new without requiring that I tab away from the combo box?
I have included sample code.
import Tix
import tkMessageBox
class App(object):
def __init__(self, window):
window.winfo_toplevel().wm_title("test")
self.window = window
self.combo = Tix.ComboBox(window)
self.combo.insert(Tix.END, 'thing1')
self.combo.insert(Tix.END, 'thing2')
self.combo.entry['state'] = "normal"
self.combo['editable'] = True
self.combo.pack()
button = Tix.Button(window)
button['text'] = "Go"
button['command'] = self.go
button.pack()
def go(self):
tkMessageBox.showinfo('info', self.combo['value'])
if __name__ == '__main__':
root = Tix.Tk()
App(root)
root.mainloop()
woo!
solved it on my own.
Use
self.combo['selection']
instead of
self.combo['value']
NOTE: copy of Moe's answer that can be selected as chosen answer
woo!
solved it on my own.
Use
self.combo['selection']
instead of
self.combo['value']

Categories