سؤال

In my game, I have an __init__ function which creates a set of seven entry boxes, like so:

self.string1 = StringVal()
self.entry1 = Entry(frame, textvariable = self.string1).grid(row = 4, column = 1, sticky = W)

This is copied six more times. This works. At the end of the game, though, I want to delete the Entry box's text, using this code I found several places online:

self.entry1.delete(0, END)

I also tried using something else I found:

if self.entry1.get():
    self.entry1.delete(0, END)

These both say that self.entry1 is a NoneType object, and has no method .get() or .delete(). Just to try things out, I substituted self.entry1.get() and self.entry1.delete(0,END) with self.string1.get(), etc. I also tried substituting .delete(0, END) with .delete(0.0, END). Neither of these worked either. I do not understand what I am doing wrong. Thanks for your help!

هل كانت مفيدة؟

المحلول

When you do something like this:

self.foo = Button(...).grid(...)

... Then what gets stored in self.foo is the result of the call to grid(). This will always be None. You need to separate your widget creation from the loyout in order to save a reference to the created widgets.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top