I’m having problems with the following code (I am a beginner in most things Python related) and am not sure how to use ‘self’ in this sense. I simply would like to retrieve any values given in the Entry boxes I create (bearing in mind there is a different amount of boxes depending on the if statement) when the start button is clicked.
I am however getting errors with ‘self’ not defined and such like. Is there any way of keeping the loops I’ve made and getting all the values back or is there a simpler way of writing this?
def ChBox(self, ch):
column = 0
if ch == "List":
i = [0, 1, 2, 3]
for i in i:
ch_title = Label(ch_frame, text='Value %g'% float(i+1), bg='white', borderwidth=0, width=0)
ch_title.grid(row=0, column=column+i, sticky="nsew", padx=1, pady=1)
self.box = Entry(ch_frame, bg='white', borderwidth=0, width=0)
self.box.grid(row=1, column=column+i, sticky="ew", padx=1, pady=1)
elif ch == "Inter":
i = [0, 1]
ch_title1 = Label(ch_frame, text='Min Value', bg='white', borderwidth=0, width=0)
ch_title1.grid(row=0, column=column, sticky="nsew", padx=1, pady=1)
ch_title2 = Label(ch_frame, text='Max Value', bg='white', borderwidth=0, width=0)
ch_title2.grid(row=0, column=column+1, sticky="nsew", padx=1, pady=1)
for i in i:
self.box = Entry(ch_frame, bg='white', borderwidth=0, width=0)
self.box.grid(row=1, column=column+i, sticky="nsew", padx=1, pady=1)
def StartBut(self):
value = self.box.get()
but1 = Button(frame_but, text='Start', command =StartBut(self))
UPDATE: In fact what I would like to do is return the box values without having to use classes or self if possible? I’m afraid that introducing classes and self etc now into my code would take too much time to rewrite it all..
Ok, just realised from reading comments that the
Buttonclass here is from tkinter.This changes the nature of the question a little.
The
commandargument of theButtonconstructor is a callable with no arguments.This means that if you just pass in a function, you can’t actually pass any arguments to that function.
So if you want that function to have access to the but1 instance, then you will need to do something else.
One thing is to wrap the whole thing in a class, eg:
This will all depend on what you’re trying to achieve, and how you are designing things.
Also, I’m not sure what the ChBox function is trying to achieve.
Old answer
In python, “self” is usually the argument name used for class methods.
For example, if you have the following:
then when you write:
the call to
f.echo(2), it gets translated into something likeFoo.echo(f, 2), so the instancefbecomes the first argument, and hence is bound toself, and 2 is the second argument, bound toa.In fact, you could actually call
Foo.echo(f, 2)and it will do the exact same thing asf.echo(2).I’m not sure what your
Buttonclass is doing, but supposing it does something like:And what you want to achieve is to pass the current
Buttoninstance to the command, you should write instead:and when constructing an instance:
You probably also want to rename the argument of StartBut, as
selfis normally only used as the first argument of methods:Sidenote
Understanding that
instance.method(args)translates toClass.method(instance, args)will go a long way to helping you understand a lot of python semantics.For instance, when learning to join a list into a string in python, you often see
' '.join(["hello", "world"]).A lot of novices find this confusing and complain about the inconsistent syntax of python.
But really, this is the hugely consistent with the rest of python — all it is doing is calling
str.join(' ', ["hello", "world"]), and this should look more like the familiar form of join found in other languages.' '.join(["hello", "world"])is just a pythonic way of calling this method.