Display radio button horizontally using ipywidgets - jupyter-notebook

I would like to display radio buttons horizontally using ipywidgets.
radio_input1 = widgets.RadioButtons(options=['Option 1', 'Option 2'])
But it shows the radio button vertically :
One hack i was trying to put radio buttons with only one option in HBox, and adding observer event to each radio button, then deselect the selected radio button from the observer method but before that unobserving the event then re-register the observe event. Somehow its calling 3 times:
output_radio_selected = widgets.Text() # Used to take the user input and access it when needed
radio_input1 = widgets.RadioButtons(options=['Option 1', 'Option 2']) # Declare the set of radio buttons and provide options
radio_input2 = widgets.RadioButtons(options=['Option 3', 'Option 4'])
def bind_selected_to_output(sender): # Connect the input from the user to the output so we can access it
#radio_input1.unobserve(bind_selected_to_output)
radio_input1.unobserve_all()
radio_input1.index=0
#print(sender)
global selected_option # Global variable to hold the user input for reuse in your code
output_radio_selected.value = radio_input1.value
selected_option = output_radio_selected.value # Example variable assigned the selected value
print('Selected option set to: ' + selected_option) # For test purposes
radio_input1.observe(bind_selected_to_output)
radio_input1.observe(bind_selected_to_output) # Run the bind... function when the radio button is changed
#radio_input1.observe(bind_selected_to_output, names=['value'])
#radio_input1 # Display the radio buttons to the user
widgets.HBox([radio_input1])

I could do it with possible hack, i don't know its right solution but it works.
I took 4 different radio buttons and HBox
widgets.HBox([radio1,radio2,radio3,radio4])
Then after selecting one radio button i m de-selecting the other radio button which is selected.
Here how i done :
import ipywidgets as widgets
import numpy
output_radio_selected = widgets.Text()
radio1 = widgets.RadioButtons(options=['Option 1'])
radio2 = widgets.RadioButtons(options=['Option 2'])
radio3 = widgets.RadioButtons(options=['Option 3'])
radio4 = widgets.RadioButtons(options=['Option 4'])
radio1.index = None
radio2.index = None
radio3.index = None
radio4.index = None
def radio1_observer(sender):
#print(sender)
radio2.unobserve(radio2_observer, names=['value'])
radio2.index = None
radio3.unobserve(radio3_observer, names=['value'])
radio3.index = None
radio4.unobserve(radio4_observer, names=['value'])
radio4.index = None
global selected_option
output_radio_selected.value = radio1.value
selected_option = output_radio_selected.value
print('Selected option set to: ' + selected_option)
radio2.observe(radio2_observer, names=['value'])
radio3.observe(radio3_observer, names=['value'])
radio4.observe(radio4_observer, names=['value'])
def radio2_observer(sender):
radio1.unobserve(radio1_observer, names=['value'])
radio1.index = None
radio3.unobserve(radio3_observer, names=['value'])
radio3.index = None
radio4.unobserve(radio4_observer, names=['value'])
radio4.index = None
global selected_option2
output_radio_selected.value = radio2.value
selected_option2 = output_radio_selected.value
print('Selected option set to: ' + selected_option2)
radio1.observe(radio1_observer, names=['value'])
radio3.observe(radio3_observer, names=['value'])
radio4.observe(radio4_observer, names=['value'])
def radio3_observer(sender):
radio1.unobserve(radio1_observer, names=['value'])
radio1.index = None
radio2.unobserve(radio2_observer, names=['value'])
radio2.index = None
radio4.unobserve(radio4_observer, names=['value'])
radio4.index = None
global selected_option3
output_radio_selected.value = radio3.value
selected_option3 = output_radio_selected.value
print('Selected option set to: ' + selected_option3)
radio1.observe(radio1_observer, names=['value'])
radio2.observe(radio2_observer, names=['value'])
radio4.observe(radio4_observer, names=['value'])
def radio4_observer(sender):
radio1.unobserve(radio1_observer, names=['value'])
radio1.index = None
radio2.unobserve(radio2_observer, names=['value'])
radio2.index = None
radio3.unobserve(radio3_observer, names=['value'])
radio3.index = None
global selected_option4
output_radio_selected.value = radio4.value
selected_option4 = output_radio_selected.value
print('Selected option set to: ' + selected_option4)
radio1.observe(radio1_observer, names=['value'])
radio2.observe(radio2_observer, names=['value'])
radio3.observe(radio3_observer, names=['value'])
radio1.observe(radio1_observer, names=['value'])
radio2.observe(radio2_observer, names=['value'])
radio3.observe(radio3_observer, names=['value'])
radio4.observe(radio4_observer, names=['value'])
widgets.HBox([radio1,radio2,radio3,radio4])

I find this solution useful, which worked for me. ref link
%%html
<style>
.widget-radio-box {
flex-direction: row !important;
}
.widget-radio-box label{
margin:5px !important;
width: 120px !important;
}
</style>

Based on gavaskar's comment, add this code to the end of your .ipynb file:
HTML(
value = "<style>.widget-radio-box {flex-direction: row !important;}.widget-radio-box label{margin:5px !important;width: 120px !important;}</style>"
)

Related

QPalette and app.setStyleSheet() not impacting child Widget colors

I've got a pretty large PyQt5 application that I'm finally polishing with some colors. I've created a QPalette() and pass that to the app at launch. For the most part, it works (yay!). But not all of the child widgets are picking up the QPalette settings, and so I added a StyleSheet, but that again isn't working consistently. The only way I've been able to impact a widget color is by directly adding it when the widget is created, which is fine in something small.
The Main window:
class MyBigApplication(QMainWindow, QWidget):
def __init__(self):
super(MyBigApplication, self).__init__()
# ... load the various pieces, which include prompting user login and making some background connections.
# Build the GUI
def init_ui(self):
self.statusBar().showMessage('Welcome to MyBigApplication!')
self.grid_layout = QGridLayout()
# Initialize tab screen
self.tabs = QTabWidget()
self.tabs.setTabShape(QTabWidget.Triangular)
self.foo = fooTab(self, self.tabs)
self.bar = barTab(self, self.tabs)
self.baz = bazTab(self, self.tabs)
self.grid_layout.addWidget(self.tabs,0,1,4,1)
main_widget = QWidget()
main_widget.setLayout(self.grid_layout)
self.setCentralWidget(main_widget)
# Additional setup of menus and such
if __name__.endswith('__main__'):
app = QCoreApplication.instance()
while app is not None:
app.close()
app = QApplication(sys.argv)
app.setStyle('Fusion')
dark_palette = QPalette()
# Define some colors to get started
light_grey = QColor(243,243,243)
medium_grey = QColor(211,216,219)
dark_grey = QColor(52,59,64)
dark_palette.setColor(QPalette.Window, QColor(dark_grey))
dark_palette.setColor(QPalette.AlternateBase, QColor(medium_grey))
dark_palette.setColor(QPalette.Button, QColor(dark_grey))
dark_palette.setColor(QPalette.Base, QColor(25, 25, 25)) # almost black
dark_palette.setColor(QPalette.Link, QColor(green))
dark_palette.setColor(QPalette.Highlight, QColor(half_green))
dark_palette.setColor(QPalette.WindowText, QColor(light_grey))
dark_palette.setColor(QPalette.ToolTipBase, QColor(light_grey))
dark_palette.setColor(QPalette.ToolTipText, QColor(light_grey))
dark_palette.setColor(QPalette.Text, QColor(light_grey))
dark_palette.setColor(QPalette.ButtonText, QColor(light_grey))
dark_palette.setColor(QPalette.BrightText, Qt.red)
dark_palette.setColor(QPalette.HighlightedText, Qt.black)
app.setPalette(dark_palette)
app.setFont(QFont('Franklin Gothic Book', 9))
app.setStyleSheet("""
QMainWindow {
}
QToolTip {
color: #f3f3f3;
background-color: #2a82da;
border: 1px solid white;
}
QTableView { # This works
border: 1px solid #218a21
}
QPushButton { # And this works
padding: 10px 15px 10px 15px,
}
QPushButton:hover { # But this does not
background-color: red,
}
QTableView::item:alternate { # And this also does not
background-color: #d3d8db,
}
""")
execute = MyBigApplication()
sys.exit(app.exec_())
The fooTab includes tables of data:
class fooTab(QWidget):
def __init__(self, parent, tabs):
super(fooTab,self).__init__()
self.root = parent
self.tabs = tabs
def init_foo_one(self):
self.foo_tab = QWidget()
self.tabs.addTab(self.foo_tab, 'FOO')
tab_layout = QVBoxLayout()
foo_id_box = QGroupBox('FOO DATA')
clear_button = QPushButton('Clear Foo Data Table')
clear_button.clicked.connect(self.clear_foo_table)
# Set up the Table Model/View/Proxy
self.foo_id_table = QTableView()
self.foo_data_model = fooTableModel() # This is a QAbstractTableModel class
self.foo_data_model.setDataDict(data)
self.foo_id_table_columns = ['1','2','3','4']
self.foo_resizable_cols = [0,1,2,3,4]
self.foo_data_model.setDataHeader(self.foo_id_table_columns)
self.foo_table_proxy.setSourceModel(self.foo_data_model)
self.foo_id_table.setModel(self.foo_table_proxy)
self.foo_id_table.setSelectionBehavior(QAbstractItemView.SelectRows)
self.foo_id_table.setSortingEnabled(True)
self.foo_id_table.setWordWrap(False)
self.foo_id_table.setAlternatingRowColors(True)
# Create a layout for that box using a grid
foo_id_box_layout = QGridLayout()
# Add the widgets into the layout
foo_id_box_layout.addWidget(self.foo_id_table,0,0,1,5)
foo_id_box_layout.addWidget(clear_button,1,2,1,1)
# Setup the layout to be displayed in the box
foo_id_box.setLayout(foo_id_box_layout)
tab_layout.addWidget(foo_id_box)
self.foo_tab.setLayout(tab_layout)
The bazTab:
class BazTab(QWidget):
def __init__(self, parent, tabs):
super(BazTab,self).__init__()
self.root = parent
self.tabs = tabs
self.h1_font = QFont()
self.h1_font.setBold(True)
self.h1_font.setPointSize(16)
self.h2_font = QFont()
self.h2_font.setBold(False)
self.h2_font.setPointSize(12)
self.h3_font = QFont()
self.h3_font.setBold(True)
self.init_ui()
def init_ui(self):
self.component_tab = QScrollArea()
self.tabs.addTab(self.baz_tab, 'Baz')
self.tab_layout = QHBoxLayout()
self.component_tab.setLayout(self.tab_layout)
component_button_box = QGroupBox('Various Buttons')
component_button_layout = QVBoxLayout()
component_button_layout.setAlignment(Qt.AlignTop)
component_button_box.setLayout(component_button_layout)
self.tab_layout.addWidget(component_button_box)
first_button = QPushButton('Request #1')
first_button.clicked.connect(self.request_one)
component_button_layout.addWidget(first_button)
second_button = QPushButton('Request #2')
second_button.clicked.connect(self.request_two)
component_button_layout.addWidget(second_button)
# Several more buttons created in here
# None of these buttons look like the buttons in the info dialog (which have a color from the QPalette)
I can manually edit the QTableView to show alternating colors, only if I add it to each instance.
I cannot get some of the QPushButtons to change, even when I add each instance's styleSheet.
Using the QPalette has saved a ton, by not having to modify widget by widget. For the extra details, I'm fine to use the app.setStyleSheet except that it doesn't always work.
Is it me, or is this just the way it is?

Dash Button n-clicks not triggering callback

I've tried both dcc.Button and dbc.Button. Same issue. When I click the button, it shows that it is clicked (highlighted, border), but is not triggering the callback. It stays in the clicked state indefinitely. I've tested my backend code separate from dash and it works fine.
I'm trying to dynamically add a div to the side-bar content, but my other callback has the same issue.
#app.callback(
Output('side-bar', 'children'),
Input("login-button", "n-clicks"),
[State("username", "value"),
State("password", "value"),
State("side-bar", "children")])
def get_project_reports(clicks, un, pw, children):
if clicks is None or un is None or pw is None:
raise PreventUpdate
else:
....DO STUFF....
option_list = df_proj_list["Project Title"].to_list()
select_project = html.Div([
dbc.Label("Select Project"),
html.Hr(),
dbc.Select(
option_list,
id={
'type': 'filter-dropdown',
'index': clicks
})
])
children.append(select_project)
return children

dynamic create checkbox as array size in swift

I Want to create a view with dynamic create checkbox i know how to create c checkbox dynamic but how to manage size of view?
Here is my Code:
var spacer: CGFloat = 23
for comments in fixer as NSArray {
var counter = ""
print(comments)
counter = comments as! String
let button = UIButton(frame:CGRect(x: 8, y: 0+spacer , width: 20, height: 20))
button.setTitle("hi", for: .normal)
self.assignfixerView.addSubview(button)
let label = UILabel(frame:CGRect(x: 40, y: 0+spacer , width: 80, height: 22))
label.text = counter
self.assignfixerView.addSubview(label)
spacer = spacer + 23
}
Here is my view :
in this user are not fix and i want status(label) and textfield position change and if i create this button than how's the selection process?
Please give me better /suggestion if any better controller than it will be ok
OR any dropdown with multiple selection and after selection all will be display in textfield
Thank You in Advanced!

Change text selection color for TextArea in Flex 4 for programmatic selection

I am using a Flex 4 spark TextArea and I implemented a search field. I use the following code to highlight the searched text in the TextArea:
private function findAndHighlightText():void
{
var text:String = findTextInput.text;
var beginIndex:int = scriptSourceTextArea.text.indexOf( text, m_lastFoundIndex );
if (beginIndex == -1 && m_lastFoundIndex > 0)
{
// We are at the end, search back from the begin
Alert.show( resourceManager.getString( 'groovyresources', 'search.at.end' ),
resourceManager.getString( 'groovyresources', 'find' ) );
m_lastFoundIndex = 0;
beginIndex = scriptSourceTextArea.text.indexOf( text, m_lastFoundIndex );
}
if (beginIndex != -1)
{
var endIndex:int = beginIndex + text.length;
m_lastFoundIndex = endIndex;
scriptSourceTextArea.selectRange( beginIndex, endIndex );
scriptSourceTextArea.scrollToRange( beginIndex );
}
else
{
Alert.show( resourceManager.getString( 'groovyresources', 'search.not.found', [text] ),
resourceManager.getString( 'groovyresources', 'find' ) );
}
}
The most important is the method selectRange on the TextArea. This highlights the text in the TextArea, but I would like to use a different color.
I can change the highlight color for manual selection by applying the CSS style focused-text-selection-color (See http://www.kirupa.com/forum/showthread.php?354479-Change-highlight-color-for-text-fields), but this does not change the color for the programmatic selection.
UPDATE: The color does not change for the programmatic selection because the TextArea does not have the focus at that moment. So I need to find the unfocused selection color CSS name.
There are 2 styles that influence how the selected text is highlighted in Flex:
.scriptSourceTextArea {
focused-text-selection-color: #788ec5;
unfocused-text-selection-color: #7e94cb;
}
One is for focused, other is for when the text area does not have the focus.

ExtJs Grid acting very strange

So I have two pictures of the weirdness that is occuring
As you can see in the picture above, the scroll bar on the right hand side is being cut off a little bit by the screen, and even when you scroll to the right, you don't get the bar back, it remains cut off.
Here is the other scenario:
Here, you can see that when I scroll down in this grid, the scroll bar kind of fits into the bottom of the grid and doesn't even go all the way down. You need to manually click into the grid and hit the down arrow to get the rest of the way down.
What could be causing both of these weird issues?
Edit:
Here is the code to generate the grid (Ext created through VB controls):
Dim VehicleOptionsGrid As New Akcelerant.Framework.WebControls.Grids.Grid
With VehicleOptionsGrid
.ID = "VehicleOptionsGrid"
.Title = "Vehicle Options"
.Toolbar.UseDefaultButtons = False
.Mode = Grids.Grid.GridMode.Control
.Panel.Border = False
.Panel.Style = "border-width:1px;margin-bottom:5px"
.Ref = "../../../../../VehicleOptionsGrid"
.Editable = True
With .Columns.Add("IsSelected", "Selection", Akcelerant.Framework.WebControls.Grids.Grid.ColumnDataType.Boolean)
.Renderer = "renderVehicleCheckbox"
End With
.Columns.Add("CollateralId", "").Hidden = True
.Columns.Add("OptionId", "OptionId").Hidden =True
.Columns.Add("OptionName", "Name").Width = 200
.GridHeight = 400
.DataBind()
ViewResponse.AddScript(.ToString(False))
ViewResponse.AddScript("VehicleOptionsGrid.grid.addListener('cellclick', changeOptionStatus);")
End With
Here is where we add the grid to the page:
With .AddPanel
With .AddPanel
.Title = ""
.Html = "Standard Options are preselected. Please select additional options as needed."
.Style = "padding-bottom:5px"
End With
.Ref = "../../../../VehicleOptionsPanel"
.Title = "Vehicle Options"
.Frame = True
.Style = "padding-bottom:5px"
.Layout = Pages.Panel.LayoutType.Column
.Height = 400
.Collapsed = True
.AddExtObject("VehicleOptionsGrid.grid")
End With
It seems that the grid sizes are higher than the actual component that holds the grid (a collapsible panel).

Resources