문제

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