我希望用户能够通过Tkinter窗口输入文件列表。我从一个条目和一个要求文件的按钮开始。用户选择文件后,我想编辑条目并在其下面添加另一个Entry-Button对。
这是我现在的设置:
class ImagesFrame(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
self.controller = controller # This is the class I use to represent the application, it extends Tk
self.entries = {}
""":type: dict[int, Entry]"""
self.files = {}
""":type: dict[int, basestring]"""
self.row = 1
Label(self, text="Add image files").grid(row=0, column=0)
self.add_entry()
if self.row in self.files:
self.entries[self.row].config(text=self.files[self.row])
self.add_entry()
self.row += 1
def add_entry(self):
self.entries[self.row] = Entry(self)
self.entries[self.row].grid(row=self.row, column=0)
Button(self, text='...', command=self.ask_file).grid(row=self.row, column=1)
def ask_file(self):
path = tkFileDialog.askopenfilename(title='Choose image file', initialdir=self.controller.plugin.path)
self.files[self.row] = path
self.controller.update() # again, controller is an instance of a class inheriting from Tk
似乎self.controller.update()
行没有像我预期的那样工作。我希望它重新绘制框架,然后if self.row in self.files:
将成立。