我是Python的初学者。 我想创建多个列表框并从列表框中读取条目。列表框的数量取决于在代码开头定义的名为“result”的列表的大小。列表“结果”的长度不是常量。根据列表框中的选择,需要进一步操作。
我最终的代码就像:
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
我只使用此代码获得一个列表框,但我获得了选定数量的标签 在此之后我被困住了。无法再进一步了。请帮我。提前致谢。如果信息不清楚,请告诉我。我也对一个全新的代码持开放态度。
答案 0 :(得分:1)
您的代码(经过少量修改)适合我。
我不知道你为什么会有问题。
我把我的工作代码
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()