0

I am a beginner in Python. I wanted to create multiple listboxes and read the entries from the listboxes. The number of list-boxes depends on the size of the list named "result" defined at the start of the code. The length of the list "result" is not a constant. Depending on the selections made in the listboxes further operations need to be formed.

The code I ended up is like:

result = ['Weekly','Monthly',Annual]
class Application(Frame):

def __init__(self,master):
    Frame.__init__(self,master)
    self.grid()
    self.create_widgets()

def create_widgets(self):
    for inst in result:



        textenter = "Select the required output format" + inst

        self.Label[inst] = Label(self,text = textenter)
        self.Label[inst].grid(columnspan = 2, sticky = W)

        self.Listbox[inst] = Listbox(self, selectmode = MULTIPLE,exportselection = 0)
        self.Listbox[inst].grid(sticky = W)


        for items in ["Text","XML","HTML"]:
            self.Listbox[inst].insert(END,items)



    self.submit_button = Button(self, text = "Submit",command = self.returns)
    self.submit_button.grid(row = 7, column = 1, sticky = W)

    self.content = []



def returns(self):
    for inst in result:
        self.content.append(self.Listbox[inst].curselection())
        print self.content


    self.master.destroy()    


root = Tk()

app = Application(master = root)
root.title("Output Formats")
app.mainloop()
print app.content

I get only one listbox with this code but I get the selected number of labels I am stuck after this point. Couldn't get any further. Please help me. Thanks in advance. Please let me know if the info is not clear. I am open to a completely new code as well.

2
  • btw: use dictionary self.listbox[inst] in place of self.("Listbox"+inst) Commented Nov 26, 2015 at 17:42
  • Changed as you said. Edited in my post too. No improvements yet Commented Nov 26, 2015 at 17:49

1 Answer 1

1

Your code (with small modifications) works for me.

I don't know why you have a problem.

I put my working code

import Tkinter as tk

result = ['Weekly', 'Monthly', 'Annual']

class Application(tk.Frame):

    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.listbox = dict() # lower case for variable name 
        self.label = dict() # lower case for variable name 

        for inst in result:
            #textenter = "Select the required output format" + inst
            textenter = inst

            self.label[inst] = tk.Label(self, text=textenter)
            self.label[inst].grid(columnspan=2, sticky=tk.W)

            self.listbox[inst] = tk.Listbox(self, selectmode=tk.MULTIPLE, exportselection=0)
            self.listbox[inst].grid(sticky=tk.W)

            for items in ["Text", "XML", "HTML"]:
                self.listbox[inst].insert(tk.END,items)

        self.submit_button = tk.Button(self, text="Submit", command=self.returns)
        self.submit_button.grid(row=7, column=1, sticky=tk.W)


    def returns(self):
        self.content = []

        for inst in result:
            self.content.append(self.listbox[inst].curselection())

        print self.content

        self.master.destroy()    


root = tk.Tk()
app = Application(root)
root.title("Output Formats")
app.mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

Its working now. I guess its the row and column number which caused the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.