简体   繁体   中英

How can I save the input for multiple textbox dialog in Python?

I am trying to build a dialog window in ironpython that offers multiple textbox for user input for a table with multiple columns. However, I am having difficulty to pass the user input values to another function. My code looks like following:

self.newVal = []
for column in columns:
            self.label = Label()
            self.label.Text = col
            self.label.Location = Point(10, offset)
            self.cb = Textbox()
            self.cb.Text = "Type in new value"
            self.cb.Location = Point(150,offset)
            self.cb.Height = 20
            self.cb.Width = 295
            self.cb.Enabled = True
            self.Controls.Add(self.label)
            self.Controls.Add(self.cb)
            self.newVal.append(self.cb.Text)
            offset = offset + 30

The problem is that the self.newVal passed from this code is just the original message of "Type in new value", it does not really take the user input values in the text box, when called by another function. Actually, if refer to self.cb.Text from another function, the value is what the user has newly typed. But this won't work for me because self.cb.Text only gives the input for the last column. While self.newVal contains all column values, except it is not updated with the user input as mentioned.

Another related question. I attempted to get around with this issue by defining multiple Textbox and pass them out individually. But I can't find a way of using a variable after self. . For example, in the above code, if I use self.col instead of self.cb , the code doesn't really treat col as a variable here, all I get is a single self.col with col being a string, exactly the same as using self.cb . Is it possible to use a variable after self. in a class?

Thank you furas! Using self.all_cb.append(self.cb) instead of self.newVal.append(self.cb.Text) solved the problem.

As furas mentioned, you will need to add each initialized TextBox to a list, or create a new variable for each TextBox. For example:

self.textbox_list = []
for _ in columns:
    textbox = TextBox()
    self.textbox_list.append(textbox)

Though I would prefer to reference a dictionary , where the keys are the columns:

self.textbox_dict = {}
for i in range(columns):
    textbox = TextBox()

    # Here you can manipulate the textbox

    self.textbox_dict[i] = textbox

# Then reference each TextBox by column number
val = self.textbox_dict[0].Text

You could also create a class function to initialize the table. Here I also show an example of how to create the TextBox object variable names dynamically using setattr.

class MyClass:
    columns = 8
    self.init_table(columns)

    # Then you can reference the textbox by variable name
    val = self.textbox_0.Text

    def init_table(self, cols):
        for i in range(cols):
           setattr(self, "textbox_{}".format(i), TextBox())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM