SOLVED: help understanding __init__ and passing arguments

A forum for general discussion of the Python programming language.

SOLVED: help understanding __init__ and passing arguments

Postby TheFunk » Mon Sep 16, 2013 6:04 pm

Sorry I haven't really contributed much just yet. In the future I'll try to start balancing my questions with the help I offer. For now though, I have a few questions about the __init__ method/function thing and about passing arguments properly.

I have two separate .py files. One contains my GUI environment, and the other holds the functions used by that environment. I imported the class with the functions into the GUI file and from the GUI created an object of said class. However when I go to run a method/function from within the GUI, I get an error letting me know...

Code: Select all
TypeError: ckbutton1() takes 0 positional arguments but 1 was given
Traceback (most recent call last):
  File "C:\Users\funk\Documents\program.py", line 125, in <module>
    sys.exit(app.exec_())
SystemExit: 0


I've tried counteracting this by using *args as an argument when defining my functions, but this doesn't seem to help.

I've been told before that I need to use 'self' so I tried using it as a lone argument, and the results were more of the same. I then learned about the __init__ function, which to me looks like it should just do initial tasks for you, such as alias 'self.object' to 'object', or something like that. In pseudo-code, here's a piece of my two files.

Code: Select all
   
class exampleclass:

    def __init__(self):
            # Do some initial text doodad.
            # potentially important
    def ckbutton1(self):
        print("test test test")


and the GUI...

Code: Select all
    example = exampleclass()
    when button 'pressed': example.chkbutton1


Anyone out there have the time to clear this up for me? I read through the documentation on passing arguments and it was weird to me, to say the least. Also, I apologize if any of this doesn't make much sense, I'm incredibly tired today.
Last edited by TheFunk on Tue Sep 17, 2013 2:29 pm, edited 1 time in total.
TheFunk
 
Posts: 27
Joined: Fri Aug 30, 2013 5:46 pm

Re: help understanding __init__ and passing arguments

Postby Yoriz » Mon Sep 16, 2013 6:45 pm

If ckbutton1 is bound to a gui event, when the event happens ie the button is pressed,
Gui frameworks often pass an argument giving details about the event that happened(You dont mention which gui framework your using or give much away about it).
if this is the case, your ckbutton1 method is not created in a way that its able to recieve the argument.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 842
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: help understanding __init__ and passing arguments

Postby TheFunk » Mon Sep 16, 2013 7:59 pm

In this case I'm using PyQt4. I learned Tkinter a while back and found that it was rather intuitive, and easy to pick up. However, in order to make a large GUI application with multiple windows and complex widgets, the only time friendly option was to go with a library that has a nice GUI builder. (Qt Designer). I used pyuic4 to convert the .ui file to a .py file.

Here's the exact wording from the GUI side that calls the function but draws that error:
Code: Select all
QtCore.QObject.connect(self.box1, QtCore.SIGNAL(_fromUtf8("pressed()")), diagnostics.ckbutton1)


diagnostics is the name of an object I created from the class I imported.
TheFunk
 
Posts: 27
Joined: Fri Aug 30, 2013 5:46 pm

Re: help understanding __init__ and passing arguments

Postby Yoriz » Mon Sep 16, 2013 8:39 pm

As i said in the previous post
Code: Select all
def ckbutton1(self):

must receive the event as its first argument.
Code: Select all
def ckbutton1(self, event):
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 842
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: help understanding __init__ and passing arguments

Postby TheFunk » Tue Sep 17, 2013 12:37 pm

I thought that too, but for some reason, regardless of how many arguments I put in the definition, when I click the button and the GUI makes the call to the function, IDLE returns that "ckbutton1 takes 0 positional arguments but 1 was given".

A weird thing I've noticed is that if called like this: class.ckbutton1, the program will continue to run even after I press the button, and the error will just continue to reappear on the screen.

However if I call the function like this: class.ckbutton1(), the program gives the 0 but 1 given error, and then immediately crashes before loading the GUI.

If called like this: class.ckbutton1('foo') the program gives a 0 but 2 given error, confirming that the framework is sending it's own argument as you said earlier, and also immediately crashes.

Why might my function be expecting 0 arguments, when it's set to expect 2?
TheFunk
 
Posts: 27
Joined: Fri Aug 30, 2013 5:46 pm

Re: help understanding __init__ and passing arguments

Postby stranac » Tue Sep 17, 2013 12:47 pm

This is really not a good way to get help.
We have to guess what t is you have in your code, and then you are guessing what it is we are saying.

A better way:
You post your actual code and the exact erroe traceback. (If you can recreate the error with a short code sample, that's even better, but make it runnable code)
Then we'll know exactly what the problem is, and we can tell you what you need to change.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: help understanding __init__ and passing arguments

Postby TheFunk » Tue Sep 17, 2013 2:28 pm

I would post the whole code but....

AHHHHHHH I'M AN IDIOT.

I'm sorry. I just completely wasted your time. Everything was right to begin with, I was importing from an old copy of the file that held incomplete versions of the functions, hence why even after I added 50 billion arguments to ckbutton1 it was still expecting 0 arguments. I just updated the original copy of the file that I had been importing functions from and viola. Case closed! The lesson to take away here is, always name your copied files 'filename-copy'.

Well this is embarrassing. I'll mark the thread solved.
TheFunk
 
Posts: 27
Joined: Fri Aug 30, 2013 5:46 pm


Return to General Discussions

Who is online

Users browsing this forum: Bing [Bot] and 4 guests