flexxui / flexx

Write desktop and web apps in pure Python
http://flexx.readthedocs.io
BSD 2-Clause "Simplified" License
3.25k stars 257 forks source link

Updating ui.LineEdit with ui.RadioButton #376

Closed andressaapio closed 7 years ago

andressaapio commented 7 years ago

Hi there,

first of all, I'm sorry for the beginner question, but me and my colleagues are having some trouble trying to understand some Flexx features. Here it goes:

class PassValues(ui.Widget):
    def init(self):
        with ui.FormLayout():
            self.model1 = ui.RadioButton(text='jobses')
            self.model2 = ui.RadioButton(text='selkov')

    @event.connect('model1.checked')       
    def gen_linedit1(self,*events):
        self.parameters = _return_parameter(self.model1.text)
        self.value = return_value(self.model1.text)
        self.labelx = ui.Label(text='Parameters')
        for i in range(0,len(self.parameters)):
                exec("self.pasval%d = ui.LineEdit(title=self.parameter[i],text=self.value[i])" % (i))

    @event.connect('model2.checked')
    def gen_linedit2(self,*events):
        self.parameters = _return_parameter(self.model2.text)
        self.value = return_value(self.model1.text)
        self.labelx = ui.Label(text='Parameters')
        for i in range(0,len(self.parameters)):
                exec("self.pasval%d = ui.LineEdit(title=self.parameter[i],text=self.value[i])" % (i))

x=PassValues(style='height:500px')

I'm trying to use Flexx to load some models (jobses and selkov), and depending on what is chosen (ui.RadioButton), it should open ui.Label showing the name of the parameters and ui.LineEdit with their parameters values. The problem is: the ui.LineEdit does not show! Remembering that, depending on the model choice, the ui.LineEdit should change according to their parameters and values. I don't know if this this possible in Flexx; I already tried using classes both and JS, but I had the same issue.

I'd be very grateful to any explanation.

Best regards, Andressa

almarklein commented 7 years ago

It seems that the tricky part is the creating of new UI elements. Here is a (somewhat more generic) example. You should probably avoid exec(). You can do setattr('pasval%d' % i, new_lineedit) instead.

from flexx import app, event, ui

class DynamicWidgets(ui.Widget):

    def init(self):

        with ui.VBox() as self._container:
            self._but1 = ui.Button(text='more buttons!')
            self._but2 = ui.Button(text='less buttons!')

    @event.connect('_but1.mouse_click')
    def make_more_buttons(self, *events):
        with self._container:
            ui.Button(text='This is button nr %i' % len(self._container.children))

    @event.connect('_but2.mouse_click')
    def clear_buttons(self, *events):
        self._container.children = self._container.children[:2]

m = app.launch(DynamicWidgets)
app.run()
andressaapio commented 7 years ago

Thank you very much! With your example, the issue was solved :) P.S.: exec() just fine, while setattrr() didn't work for this case